#1 问题概述
Imports System.Threading
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim objTest1 As New Form1
Dim thread1 As New Thread(New ThreadStart(AddressOf objTest1.count))
thread1.Name = "线程1"
Dim objTest2 As New Form1
Dim thread2 As New Thread(New ThreadStart(AddressOf objTest2.count))
thread2.Name = "线程2"
Dim objTest3 As New Form1
Dim thread3 As New Thread(New ThreadStart(AddressOf objTest3.count))
thread3.Name = "线程3"
thread1.Start()
thread2.Start()
thread3.Start()
End Sub
Public Sub count()
Dim cnt As Integer = 1
While cnt < 6
cnt += 1
TextBox1.Text += Thread.CurrentThread.Name + "数到" + Str(cnt) + vbCrLf
Thread.Sleep(100)
End While
End Sub
End Class
为何运行无反应
这段代码不能实现主要有两个问题,这个跨线程了所以要使用invoke从主线程中修改ui,同时改完后如下会发现
Imports System.Threading
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim objTest1 As New Form1
Dim thread1 As New Thread(New ThreadStart(AddressOf objTest1.count1))
thread1.Name = "线程1"
Dim objTest2 As New Form1
Dim thread2 As New Thread(New ThreadStart(AddressOf objTest2.count1))
thread2.Name = "线程2"
Dim objTest3 As New Form1
Dim thread3 As New Thread(New ThreadStart(AddressOf objTest3.count1))
thread3.Name = "线程3"
thread1.Start()
thread2.Start()
thread3.Start()
End Sub
Public Sub count1()
Dim cnt As Integer
cnt = 0
While cnt < 6
cnt += 1
Me.Invoke(New showDelegate(AddressOf show1), cnt)
Application.DoEvents()
End While
End Sub
Public Delegate Sub showDelegate(ByVal cnt As Integer)
Public Sub show1(ByVal cnt As Integer)
TextBox1.Text += Thread.CurrentThread.Name + "数到" + Str(cnt) + vbCrLf
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
这时会发现
这主要是由于objTest继承了form1的text box1的属性但是还并没有创建text box1
这时将objTest1删除从form1调用即可成功
代码如下
Imports System.Threading
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim thread1 As New Thread(New ThreadStart(AddressOf count1))
thread1.Name = "线程1"
Dim objTest2 As New Form1
Dim thread2 As New Thread(New ThreadStart(AddressOf count1))
thread2.Name = "线程2"
Dim objTest3 As New Form1
Dim thread3 As New Thread(New ThreadStart(AddressOf count1))
thread3.Name = "线程3"
thread1.Start()
thread2.Start()
thread3.Start()
End Sub
Public Sub count1()
Dim cnt As Integer
cnt = 0
While cnt < 6
cnt += 1
Me.Invoke(New showDelegate(AddressOf show1), cnt)
Application.DoEvents()
End While
End Sub
Public Delegate Sub showDelegate(ByVal cnt As Integer)
Public Sub show1(ByVal cnt As Integer)
TextBox1.Text += Thread.CurrentThread.Name + "数到" + Str(cnt) + vbCrLf
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
但是线程名字还是无法显示,后来摸索了下在invoke中线程是主线程的名字,所以可以在while循环中添加一个变量来代表threadName
代码如下
Imports System.Threading
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim thread1 As New Thread(New ThreadStart(AddressOf count1))
thread1.Name = "线程1"
Dim objTest2 As New Form1
Dim thread2 As New Thread(New ThreadStart(AddressOf count1))
thread2.Name = "线程2"
Dim objTest3 As New Form1
Dim thread3 As New Thread(New ThreadStart(AddressOf count1))
thread3.Name = "线程3"
thread1.Start()
thread2.Start()
thread3.Start()
End Sub
Public Sub count1()
Dim cnt As Integer
Dim threadName As String
cnt = 0
While cnt < 6
cnt += 1
threadName = Thread.CurrentThread.Name
Me.Invoke(New showDelegate(AddressOf show1), threadName, cnt)
Application.DoEvents()
End While
End Sub
Public Delegate Sub showDelegate(ByVal threadName As String, ByVal cnt As Integer)
Public Sub show1(ByVal threadName As String, ByVal cnt As Integer)
TextBox1.Text += threadName + "数到" + Str(cnt) + vbCrLf
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
结果
这是可已完成目的,也可完全掌握过程中出现的错误