form1.vb

来自「Visual Basic .NET程序设计教程源代码」· VB 代码 · 共 190 行

VB
190
字号
Imports System.Threading
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim t1 As Thread
    Dim t2 As Thread
    Private Sub BackgroundProcess()
        Dim i As Integer = 1
        Do While True
            ProgressBar1.Value = i
            i += 1
            If i = 10000 Then i = 1
        Loop
        Thread.Sleep(1)
    End Sub
    Private Sub ReceiveDataProcess()
        Dim i As Integer = 1
        Do While True
            ProgressBar2.Value = i
            i += 1
            If i = 10000 Then i = 1
        Loop
    End Sub
  Private Sub ButtonBackProc_Click(ByVal Sender As System.Object,ByVal e As System.EventArgs)  Handles ButtonBackProc.Click
        t1 = New Thread(AddressOf Me.BackgroundProcess)
        t1.Start()
    End Sub
    Private Sub ButtonForntProc_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles ButtonForntProc.Click
        t2 = New Thread(AddressOf Me.ReceiveDataProcess)
        t2.Start()
    End Sub
    Private Sub ButtonStopProc_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStopProc.Click
        t1.Abort()
    End Sub
    Private Sub ButtonStopReceive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStopReceive.Click
        t2.Abort()
    End Sub
#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写 dispose 以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents ButtonBackProc As System.Windows.Forms.Button
    Friend WithEvents ButtonStopProc As System.Windows.Forms.Button
    Friend WithEvents ButtonForntProc As System.Windows.Forms.Button
    Friend WithEvents ButtonStopReceive As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
    Friend WithEvents ProgressBar2 As System.Windows.Forms.ProgressBar
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.GroupBox2 = New System.Windows.Forms.GroupBox
        Me.ButtonBackProc = New System.Windows.Forms.Button
        Me.ButtonStopProc = New System.Windows.Forms.Button
        Me.ButtonForntProc = New System.Windows.Forms.Button
        Me.ButtonStopReceive = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
        Me.ProgressBar2 = New System.Windows.Forms.ProgressBar
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.ProgressBar1)
        Me.GroupBox1.Controls.Add(Me.Label1)
        Me.GroupBox1.Controls.Add(Me.ButtonStopProc)
        Me.GroupBox1.Controls.Add(Me.ButtonBackProc)
        Me.GroupBox1.Location = New System.Drawing.Point(16, 0)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(256, 104)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "进程1"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.Add(Me.ButtonStopReceive)
        Me.GroupBox2.Controls.Add(Me.ButtonForntProc)
        Me.GroupBox2.Controls.Add(Me.Label2)
        Me.GroupBox2.Controls.Add(Me.ProgressBar2)
        Me.GroupBox2.Location = New System.Drawing.Point(16, 112)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(256, 104)
        Me.GroupBox2.TabIndex = 0
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "进程2"
        '
        'ButtonBackProc
        '
        Me.ButtonBackProc.Location = New System.Drawing.Point(16, 32)
        Me.ButtonBackProc.Name = "ButtonBackProc"
        Me.ButtonBackProc.TabIndex = 0
        Me.ButtonBackProc.Text = "数据处理"
        '
        'ButtonStopProc
        '
        Me.ButtonStopProc.Location = New System.Drawing.Point(16, 64)
        Me.ButtonStopProc.Name = "ButtonStopProc"
        Me.ButtonStopProc.TabIndex = 1
        Me.ButtonStopProc.Text = "停止处理"
        '
        'ButtonForntProc
        '
        Me.ButtonForntProc.Location = New System.Drawing.Point(16, 32)
        Me.ButtonForntProc.Name = "ButtonForntProc"
        Me.ButtonForntProc.TabIndex = 2
        Me.ButtonForntProc.Text = "接收数据"
        '
        'ButtonStopReceive
        '
        Me.ButtonStopReceive.Location = New System.Drawing.Point(16, 64)
        Me.ButtonStopReceive.Name = "ButtonStopReceive"
        Me.ButtonStopReceive.TabIndex = 3
        Me.ButtonStopReceive.Text = "停止接收"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(120, 32)
        Me.Label1.Name = "Label1"
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "数据处理进度"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(120, 32)
        Me.Label2.Name = "Label2"
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "数据接收进度"
        '
        'ProgressBar1
        '
        Me.ProgressBar1.Location = New System.Drawing.Point(104, 64)
        Me.ProgressBar1.Maximum = 10000
        Me.ProgressBar1.Name = "ProgressBar1"
        Me.ProgressBar1.Size = New System.Drawing.Size(136, 23)
        Me.ProgressBar1.TabIndex = 3
        '
        'ProgressBar2
        '
        Me.ProgressBar2.Location = New System.Drawing.Point(104, 64)
        Me.ProgressBar2.Maximum = 10000
        Me.ProgressBar2.Name = "ProgressBar2"
        Me.ProgressBar2.Size = New System.Drawing.Size(136, 23)
        Me.ProgressBar2.TabIndex = 4
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(292, 229)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.GroupBox2)
        Me.Name = "Form1"
        Me.Text = "一个多线程的例子"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region
End Class

⌨️ 快捷键说明

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