vb.net两段程序多线程运行问题 10
PublicClassForm1PublicDelegateSubVoidDelegate()'定义委托类型DimAAsDoubleDimbAsDoublePrivate...
Public Class Form1
Public Delegate Sub VoidDelegate() '定义委托类型
Dim A As Double
Dim b As Double
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim t1, t2 As Threading.Thread
t2 = New Threading.Thread(AddressOf MyThreadProc1)
t1 = New Threading.Thread(AddressOf MyThreadProc)
t2.Start()
t1.Start()
‘如果这里改为,则可以实现SetA与SteB两段程序互不干涉同时运行
Dim t1, t2 As Threading.Thread
t2 = New Threading.Thread(AddressOf SetA)
t1 = New Threading.Thread(AddressOf SteB)
t2.Start()
t1.Start()
End Sub
Public Sub MyThreadProc()
Me.Invoke(New VoidDelegate(AddressOf steA))
End Sub
Public Sub MyThreadProc1()
Me.Invoke(New VoidDelegate(AddressOf setB))
End Sub
Public Sub steA()
b = 0
Do
b = b + 1
Loop Until b > 2000000000
TextBox3.Text = CStr(b)
End Sub
Public Sub setB()
Timer1.Interval = 100
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
A = A + 1
TextBox1.Text = CStr(A)
End Sub
这是我的代码
1.如果不定义委托来控制窗体控件的话,A的自加是不会受到影响
2.一旦定义委托让A,B显示处理,则timer1,必须等到b累加到200000000后才开始执行,此时程序会等待一段时间按a才自加
3.怎么才能实现控制窗体控件时,实现多线程功能 展开
Public Delegate Sub VoidDelegate() '定义委托类型
Dim A As Double
Dim b As Double
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim t1, t2 As Threading.Thread
t2 = New Threading.Thread(AddressOf MyThreadProc1)
t1 = New Threading.Thread(AddressOf MyThreadProc)
t2.Start()
t1.Start()
‘如果这里改为,则可以实现SetA与SteB两段程序互不干涉同时运行
Dim t1, t2 As Threading.Thread
t2 = New Threading.Thread(AddressOf SetA)
t1 = New Threading.Thread(AddressOf SteB)
t2.Start()
t1.Start()
End Sub
Public Sub MyThreadProc()
Me.Invoke(New VoidDelegate(AddressOf steA))
End Sub
Public Sub MyThreadProc1()
Me.Invoke(New VoidDelegate(AddressOf setB))
End Sub
Public Sub steA()
b = 0
Do
b = b + 1
Loop Until b > 2000000000
TextBox3.Text = CStr(b)
End Sub
Public Sub setB()
Timer1.Interval = 100
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
A = A + 1
TextBox1.Text = CStr(A)
End Sub
这是我的代码
1.如果不定义委托来控制窗体控件的话,A的自加是不会受到影响
2.一旦定义委托让A,B显示处理,则timer1,必须等到b累加到200000000后才开始执行,此时程序会等待一段时间按a才自加
3.怎么才能实现控制窗体控件时,实现多线程功能 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询