mercredi 26 décembre 2007

To avoid the "Cross Thread Exception"

To avoid the "Cross Thread Exception" that happens when a thread Thread1 try to access another thread's control(Thread2, Control2), you have to do the following :

1. Add a method that change the control in Thread2, for example :

Public Sub EditControl (ByVal Text As String)
'bla blaa bla Control2.zone_text = text
End Sub

2. Add in the declarations of the Thread2, a declaration of a delgate to the previous method (a delegate is like a pointer on a method, it must have the same arguments) :

Delegate
Sub EditControlDelegate (ByVal Text As String)

3. In the Thread1, do the following to run the EditControl method of the Thread2 :

Me.Invoke (New Thread2Object.EditControlDelegate(AddressOf Thread2Object.EditControl), "This Text is the unique argument of EditControl method")

Aucun commentaire: