⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formmain.vb

📁 Visual Basic.net 实例入门
💻 VB
📖 第 1 页 / 共 2 页
字号:
        Me.GroupBox4.Size = New System.Drawing.Size(368, 64)
        Me.GroupBox4.TabIndex = 8
        Me.GroupBox4.TabStop = False
        Me.GroupBox4.Text = "运行速度"
        '
        'formMain
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(384, 345)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.GroupBox4, Me.GroupBox3, Me.GroupBox2, Me.GroupBox1, Me.btExit, Me.btStart})
        Me.Menu = Me.MainMenu1
        Me.Name = "formMain"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "多线程实例"
        CType(Me.trkSpeed, System.ComponentModel.ISupportInitialize).EndInit()
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.GroupBox3.ResumeLayout(False)
        Me.GroupBox4.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim t3 As New System.Threading.Thread(AddressOf fun3)
    Dim t2 As New System.Threading.Thread(AddressOf fun2)
    Dim t1 As New System.Threading.Thread(AddressOf fun1)
    '定义三个线程
    Private Sub subStart()
        Try
            t1.Start()
            t2.Start()
            t3.Start()
            '启动三个线程
            btStart.Enabled = False
            mStart.Enabled = False
            '将“开始”按钮设成无效
        Catch err As Exception
            '捕获并忽略异常
        End Try
    End Sub

    Private Sub btStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btStart.Click
        subStart()
        '启动三个线程
    End Sub

    Private Sub fun1()
        Dim i As Integer
        For i = 1 To 1000
            t1.Sleep(250 / trkSpeed.Value)
            '将线程延时
            prgThread1.Value = i
            '进度条向前滚动
            If i Mod 3 = 0 Then
                Label1.Text = "能被3整除的整数是:" + i.ToString
                '遇到能被3整除的整数,则把该整数显示出来
            End If
        Next
    End Sub

    Private Sub fun2()
        Dim i As Integer
        For i = 1 To 1000
            t1.Sleep(250 / trkSpeed.Value)
            '将线程延时
            prgThread2.Value = i
            '进度条向前滚动
            If i Mod 4 = 0 Then
                Label2.Text = "能被4整除的整数是:" + i.ToString
                '遇到能被3整除的整数,则把该整数显示出来
            End If
        Next
    End Sub

    Private Sub fun3()
        Dim i As Integer
        For i = 1 To 1000
            t1.Sleep(250 / trkSpeed.Value)
            '将线程延时
            prgThread3.Value = i
            '进度条向前滚动
            If i Mod 6 = 0 Then
                Label3.Text = "能被6整除的整数是:" + i.ToString
                '遇到能被3整除的整数,则把该整数显示出来
            End If
        Next
    End Sub

    Private Sub exitform()
        If t1.ThreadState <> Threading.ThreadState.Stopped Then
            If t1.ThreadState = Threading.ThreadState.Suspended Then
                t1.Resume()
            End If
            '如果线程1的状态为“Suspended”,则恢复线程1
            If t1.IsAlive Then
                t1.Abort()
            End If
            '如果线程1仍处于激活状态,则终止线程1
        End If
        '如果线程1未被终止,则终止线程1

        If t2.ThreadState <> Threading.ThreadState.Stopped Then
            If t2.ThreadState = Threading.ThreadState.Suspended Then
                t2.Resume()
            End If
            '如果线程2的状态为“Suspended”,则恢复线程2
            If t2.IsAlive Then
                t2.Abort()
            End If
            '如果线程2仍处于激活状态,则终止线程2
        End If
        '如果线程2未被终止,则终止线程2

        If t3.ThreadState <> Threading.ThreadState.Stopped Then
            If t3.ThreadState = Threading.ThreadState.Suspended Then
                t3.Resume()
            End If
            '如果线程3的状态为“Suspended”,则恢复线程3
            If t3.IsAlive Then
                t3.Abort()
            End If
            '如果线程3仍处于激活状态,则终止线程3
        End If
        '如果线程3未被终止,则终止线程3

        Close()
        '关闭窗体
    End Sub

    Private Sub btExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btExit.Click
        exitform()
        '退出应用程序
    End Sub

    Private Sub pause1()
        If btPause1.Text = "暂停" Then
            '如果按钮的文本为“暂停”,则执行暂停线程的功能
            Try
                t1.Suspend()
                '暂停线程1
            Catch err As Exception
                '捕获并忽略异常
            End Try
            btPause1.Text = "恢复"
            mPause1.Text = "恢复"
            '修改相应的菜单项和按钮的Text属性为“恢复”
        Else
            '如果按钮的文本为“恢复”,则执行恢复已暂停线程的功能
            Try
                t1.Resume()
                '恢复线程1
            Catch err As Exception
                '捕获并忽略异常
            End Try
            btPause1.Text = "暂停"
            mPause1.Text = "暂停"
            '修改相应的菜单项和按钮的Text属性为“暂停”
        End If
    End Sub

    Private Sub pause2()
        If btPause2.Text = "暂停" Then
            '如果按钮的文本为“暂停”,则执行暂停线程的功能
            Try
                t2.Suspend()
                '暂停线程2
            Catch err As Exception
                '捕获并忽略异常
            End Try
            btPause2.Text = "恢复"
            mPause2.Text = "恢复"
            '修改相应的菜单项和按钮的Text属性为“恢复”
        Else
            '如果按钮的文本为“恢复”,则执行恢复已暂停线程的功能
            Try
                t2.Resume()
                '恢复线程1
            Catch err As Exception
                '捕获并忽略异常
            End Try
            btPause2.Text = "暂停"
            mPause2.Text = "暂停"
            '修改相应的菜单项和按钮的Text属性为“暂停”
        End If
    End Sub

    Private Sub pause3()
        If btPause3.Text = "暂停" Then
            '如果按钮的文本为“暂停”,则执行暂停线程的功能
            Try
                t3.Suspend()
                '暂停线程2
            Catch err As Exception
                '捕获并忽略异常
            End Try
            btPause3.Text = "恢复"
            mPause3.Text = "恢复"
            '修改相应的菜单项和按钮的Text属性为“恢复”
        Else
            '如果按钮的文本为“恢复”,则执行恢复已暂停线程的功能
            Try
                t3.Resume()
                '恢复线程1
            Catch err As Exception
                '捕获并忽略异常
            End Try
            btPause3.Text = "暂停"
            mPause3.Text = "暂停"
            '修改相应的菜单项和按钮的Text属性为“暂停”
        End If
    End Sub

    Private Sub btPause2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btPause2.Click
        pause2()
        '暂停线程2
    End Sub

    Private Sub btPause1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btPause1.Click
        pause1()
        '暂停线程1
    End Sub

    Private Sub btPause3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btPause3.Click
        pause3()
        '暂停线程3
    End Sub

    Private Sub stop1()
        Try
            If t1.ThreadState = Threading.ThreadState.Suspended Then
                t1.Resume()
            End If
            '如果线程1的状态为“Suspended”,则恢复线程1
            If t1.IsAlive Then
                t1.Abort()
            End If
            '如果线程1仍处于激活状态,则终止线程1
        Catch err As Exception
            '捕获并忽略异常
        End Try
        btPause1.Enabled = False
        btStop1.Enabled = False
        mPause1.Enabled = False
        mStop1.Enabled = False
        '将相应的按钮和菜单项置为“无效”,以免发生异常
    End Sub

    Private Sub stop2()
        Try
            If t2.ThreadState = Threading.ThreadState.Suspended Then
                t2.Resume()
            End If
            '如果线程2的状态为“Suspended”,则恢复线程2
            If t2.IsAlive Then
                t2.Abort()
            End If
            '如果线程2仍处于激活状态,则终止线程2
        Catch err As Exception
            '捕获并忽略异常
        End Try
        btPause2.Enabled = False
        btStop2.Enabled = False
        mPause2.Enabled = False
        mStop2.Enabled = False
        '将相应的按钮和菜单项置为“无效”,以免发生异常
    End Sub

    Private Sub stop3()
        Try
            If t3.ThreadState = Threading.ThreadState.Suspended Then
                t3.Resume()
            End If
            '如果线程3的状态为“Suspended”,则恢复线程3
            If t3.IsAlive Then
                t3.Abort()
            End If
            '如果线程3仍处于激活状态,则终止线程3
        Catch err As Exception
            '捕获并忽略异常
        End Try
        btPause3.Enabled = False
        btStop3.Enabled = False
        mPause3.Enabled = False
        mStop3.Enabled = False
        '将相应的按钮和菜单项置为“无效”,以免发生异常
    End Sub

    Private Sub btStop1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btStop1.Click
        stop1()
        '终止线程1
    End Sub

    Private Sub btStop2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btStop2.Click
        stop2()
        '终止线程2
    End Sub

    Private Sub btStop3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btStop3.Click
        stop3()
        '终止线程3
    End Sub


    Private Sub mPause1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mPause1.Click
        pause1()
        '暂停线程1
    End Sub

    Private Sub mStop1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mStop1.Click
        stop1()
        '终止线程1
    End Sub

    Private Sub mPause2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mPause2.Click
        pause2()
        '暂停线程2
    End Sub

    Private Sub mStop2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mStop2.Click
        stop2()
        '终止线程2
    End Sub

    Private Sub mPause3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mPause3.Click
        pause3()
        '暂停线程3
    End Sub

    Private Sub mStop3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mStop3.Click
        stop3()
        '终止线程3
    End Sub

    Private Sub mStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mStart.Click
        subStart()
        '启动三个线程
    End Sub

    Private Sub mExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mExit.Click
        exitform()
        '退出应用程序
    End Sub

    Private Sub mAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mAbout.Click
        Dim fAbout As New formAbout()
        fAbout.Show()
        '显示“关于”窗体
    End Sub

    Private Sub formMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        t1.Priority = Threading.ThreadPriority.Normal
        t2.Priority = Threading.ThreadPriority.Normal
        t3.Priority = Threading.ThreadPriority.Normal
        '初始化线程的优先级
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -