Threading problem

S

Soheil

Hi,

Can anybody tell me why this piece of code makes my app halt?
It's just a simple form with two buttons on it!
But when I remove "Button2.Show()", everything is ok!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
ThreadPool.QueueUserWorkItem(AddressOf SomeThing)
End Sub

Private Sub SomeThing(ByVal state As Object)
Button1.Hide()
Button2.Show()
End Sub

Regards,
Soheil
 
H

Herfried K. Wagner [MVP]

* "Soheil said:
Can anybody tell me why this piece of code makes my app halt?
It's just a simple form with two buttons on it!
But when I remove "Button2.Show()", everything is ok!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
ThreadPool.QueueUserWorkItem(AddressOf SomeThing)
End Sub

Private Sub SomeThing(ByVal state As Object)
Button1.Hide()
Button2.Show()
End Sub

You cannot access instance members of Windows Forms controls from
another thread because they are not thread safe. Instead, use
'Control.Invoke' to invoke the method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top