扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
Public Class Form1
创新互联主要从事成都网站建设、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务东乡,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792
Public Class SquareClass '把多线程调用的函数封装到类中,通过类事件返回
Public Value As Double
Public Square As Double
Public Event ThreadComplete(ByVal Square As Double)
Public Sub CalcSquare()
Square = Value * Value
RaiseEvent ThreadComplete(Square)
End Sub
End Class
Dim WithEvents oSquare As SquareClass
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click '多线程返回值测试,当线程运行完成激发事件
oSquare = New SquareClass()
Dim t As New Threading.Thread(AddressOf oSquare.CalcSquare)
oSquare.Value = 30
t.Start()
End Sub
Sub SquareEventHandler(ByVal Square As Double) Handles oSquare.ThreadComplete '响应事件函数
MsgBox("The square is " Square)
End Sub
End Class
委托,Delegate
就是让你处于这个线程里时,委托另一个线程去执行一些动作
我简单举一个写richtextbox的例子:
////////////////////////////////////////////
'创建一个名为 MySubDelegate 的委托。
Delegate Sub MySubDelegate(ByVal txt As String)
'写信息到富文本主窗口
Private Sub txtW(ByVal txt As String)
Dim msgd As New MySubDelegate(AddressOf Me.txtW1)
Dim arg(0) As Object
arg(0) = txt
Me.Invoke(msgd, arg)
End Sub
'委托指向
Private Sub txtW1(ByVal txt As String)
Me.RichTextBox1.AppendText(txt)
End Sub
/////////////////////
这样,你在多线程应用时,在其他线程里用txtW(str)来写richtextbox,就不会产生错误了。不然,直接垮线程写richtextbox,可能会出现和UI线程的冲突。
Sub Main()
Dim thr As Thread
For Pi As Integer=0 To 4 //启用5线程
MulParams =Pi vbTab sFile vbTab dFile vbTab 1 vbTab DelN vbTab cr vbTab cg vbTab cb vbTab IndexI
GlobalParamas(pi)=MulParams .Split(vbTab)
thr=New Thread(AddressOf MyMulThreadCaller)
thr.Start() //启动多线程进程
Application.DoEvents
Next
End Sub
多线程一般是不推荐用的,因为线程之间如果有共享资源的话会引起竞争,需要加锁处理;而且线程间没有时序关系,所以你在调试中可能会出现异步处理结束顺序与开始处理顺序不一致的情况(我在调试中已经发现该问题)。
针对你提出的这个问题,采用了多线程处理,利用的是BackgroundWorker也就是异步处理控件进行了处理。
代码已经经过调试通过。欢迎交流,如有问题,留下QQ或其他联系方式。
代码如下,并附程序截图。
‘---------------------------------------------------
Imports System.ComponentModel '导入异步控件命名空间
Public Class Form1
Private howmany As Integer = 10
Private AnalysisNumber(0 To howmany - 1) As BackgroundWorker
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
creatNewBackgroundWorker()
addHandle()
startWork()
End Sub
Private Sub creatNewBackgroundWorker()
For i As Integer = 0 To AnalysisNumber.Length - 1
AnalysisNumber(i) = New BackgroundWorker
Next
End Sub
Private Sub addHandle()
For i As Integer = 0 To AnalysisNumber.Length - 1
AddHandler AnalysisNumber(i).DoWork, AddressOf AnalysisNumber_DoWork
AddHandler AnalysisNumber(i).RunWorkerCompleted, AddressOf AnalysisNumber_RunWorkerCompleted
Next
End Sub
Private Sub startWork()
For i As Integer = 0 To 9
Dim temp(0 To 9) As Integer
For j As Integer = 1 To 10
temp(j - 1) = 10 * i + j
Next
AnalysisNumber(i).RunWorkerAsync(temp)
Next
End Sub
Private Sub AnalysisNumber_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
Dim data As Integer()
data = CType(e.Argument, Integer())
Dim temp As Integer
For i As Integer = 0 To data.Length - 1
temp = data(i)
data(i) = temp * temp
Next
e.Result = data
End Sub
Private Sub AnalysisNumber_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
Dim data As Integer()
data = CType(e.Result, Integer())
For i As Integer = 0 To data.Length - 1
ListBox1.Items.Add(data(i))
Next
End Sub
End Class
Imports System
Imports System.Threading
Public Class Form1
Dim TestThread1, TestThread2 As Thread
Public Sub TestMethod1()
Dim i As Integer
i = 0
While (i 1000)
Label1.Text = i
i += 1
End While
End Sub
Public Sub TestMethod2()
Dim i As Integer
i = 0
While (i 1000)
Label2.Text = i
i += 1
End While
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Control.CheckForIllegalCrossThreadCalls = False
TestThread1 = New Thread(New ThreadStart(AddressOf TestMethod1))
TestThread1.Start()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Control.CheckForIllegalCrossThreadCalls = False
TestThread2 = New Thread(New ThreadStart(AddressOf TestMethod2))
TestThread2.Start()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Control.CheckForIllegalCrossThreadCalls = False
TestThread1 = New Thread(New ThreadStart(AddressOf TestMethod1))
TestThread2 = New Thread(New ThreadStart(AddressOf TestMethod2))
TestThread1.Start()
TestThread2.Start()
End Sub
End Class
定义:
Dim g_ReceiveThread As System.Threading.Thread
创建:
g_ReceiveThread = New System.Threading.Thread(AddressOf ReceiveProc)
开始:
g_ReceiveThread.Start()
结束:
Dim waitStart As Long = My.Computer.Clock.TickCount + 1000 ‘超时
While (g_ReceiveThread.ThreadState System.Threading.ThreadState.Stopped)
If My.Computer.Clock.TickCount = waitStart Then
g_ReceiveThread.Abort()
End If
Application.DoEvents()
End While
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流