Raising event from async thread

  • Thread starter Thread starter Joe Wright
  • Start date Start date
J

Joe Wright

Hello, I have a client/server application that needs to show forms depending
on what is received. The problem is that my Client class that handles all of
the sending and receiving of data raises it's DataReceived event in the
thread that is asyncronously reading the data from the tcpclient. This means
that I can't create any controls on my main form in the DataReceived event.
Does anyone know how I can raise the event in the main thread. Any help
would be appreciated. Thanks.

Joe
 
Have your Client class inherit Control. Then, in your Client class, you can
use Me.Invoke to marshal back to the main thread before you raise your
event. Then the event handler in your form will be running on the correct
thread. I used this technique here:

http://msdn.microsoft.com/library/d...ry/en-us/dndotnet/html/northwindunplugged.asp

Also, VS 2005 will solve this with a "BackgroundWorker" control. Juval has
written an implementation of that control that you can use today with VS
2003:

http://www.devx.com/codemag/Article/20639?trk=DXRSS_DOTNET

(free, but registration required)
 
Back
Top