T
Tyson Ackland
I have written a very simple threading proggie in an attempt to teach myself
threading. I have seen it referred to in articles that Forms are not thread
safe. My form has two labels which are written to by different threads in
my example. It works fine so I'm wondering if anyone can tell me what I
need to watch out for with respect to Forms and threading? For what it's
worth, here is my short proggie:
Private Sub SomeTask()
' This thread simply counts upwards, displaying the count in the
label.
Dim i As Integer
For i = 1 To 100000
Me.Label2.Text = CInt(i / 100)
Application.DoEvents()
Next i
End Sub
Private Sub SomeOtherTask()
' This thread simply counts downwards, displaying the count in the
second label.
Dim i As Integer
For i = 100000 To 1 Step -1
Me.Label3.Text = CInt(i / 100)
Application.DoEvents()
Next i
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim Thread1 As New System.Threading.Thread(AddressOf SomeTask)
Thread1.Start()
Dim Thread2 As New System.Threading.Thread(AddressOf SomeOtherTask)
Thread2.Start()
End Sub
threading. I have seen it referred to in articles that Forms are not thread
safe. My form has two labels which are written to by different threads in
my example. It works fine so I'm wondering if anyone can tell me what I
need to watch out for with respect to Forms and threading? For what it's
worth, here is my short proggie:
Private Sub SomeTask()
' This thread simply counts upwards, displaying the count in the
label.
Dim i As Integer
For i = 1 To 100000
Me.Label2.Text = CInt(i / 100)
Application.DoEvents()
Next i
End Sub
Private Sub SomeOtherTask()
' This thread simply counts downwards, displaying the count in the
second label.
Dim i As Integer
For i = 100000 To 1 Step -1
Me.Label3.Text = CInt(i / 100)
Application.DoEvents()
Next i
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim Thread1 As New System.Threading.Thread(AddressOf SomeTask)
Thread1.Start()
Dim Thread2 As New System.Threading.Thread(AddressOf SomeOtherTask)
Thread2.Start()
End Sub