Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: Spilled on December 01, 2008, 07:46 PM

Title: [VB.Net] Cross-Threading question.
Post by: Spilled on December 01, 2008, 07:46 PM
Okay I will have a class that will have 2 threads sent to it. My question is what will be the best way for those threads to access a function that accesses 2 of my controls(listbox and a trackbar) that is in my form1 class without crossthreading?

Ex:(form1.vb)

public function INeedToAccessThis() as string
'do stuff with listbox
'do stuff with trackbar
return infoneeded
end function

ex: Class1

public sub ThreadProc()
'need to call form1.INeedToAccessThis
end sub
Title: Re: [VB.Net] Cross-Threading question.
Post by: MyndFyre on December 02, 2008, 10:14 AM
All Controls implement the ISynchronizeInvoke (http://msdn.microsoft.com/en-us/library/system.componentmodel.isynchronizeinvoke.aspx) interface.  Where you need to call ThreadProc, you might code:

If Form1.InvokeRequired Then
    Form1.Invoke(AddressOf ThreadProc)
Else
    ThreadProc()
End If
Title: Re: [VB.Net] Cross-Threading question.
Post by: Spilled on December 02, 2008, 02:41 PM
Thank you