Delegate Sub MySubDelegate(ByVal txt As String,ByVal num as integer)
Private Sub txtW(ByVal txt As String,ByVal num as integer)
Dim msgd As New MySubDelegate(AddressOf Me.txtW1)
Me.Invoke(msgd, txt,num)
End Sub
Private Sub txtW1(ByVal txt As String,ByVal num as integer)
if num=1 then
TextBox1.Text=txt
else
TextBox2.Text=txt
end if
End Sub
Dim t1, t2 As Threading.Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub autoAction1()
For i = 0 To 100
Delay(10)
txtw(i.ToString,1)
Next
t1.Abort()
End Sub
Private Sub autoAction2()
For i = 0 To 100
Delay(10)
txtw(i.ToString,2)
Next
t2.Abort()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
t1 = New Threading.Thread(AddressOf autoAction1)
t1.Start()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
t2 = New Threading.Thread(AddressOf autoAction2)
t2.Start()
End Sub
Public Sub Delay(ByRef Interval As Double)? 'Interval單位為毫秒
Dim time As DateTime = DateTime.Now
Dim Span As Double = Interval * 10000
While ((DateTime.Now.Ticks - time.Ticks) < Span)
Application.DoEvents()
End While
End Sub
End Class