Raising events to run at the "main" thread

  • Thread starter Thread starter Rafael Pivato
  • Start date Start date
R

Rafael Pivato

My class launches a thread to stay listenning for a event (a serial comm
event).
When I raise the my .Net event, the handler will be running in the context
of this new thread.
In a Windows Forms application, how can I force it to run at the "main"
thread ?
 
Hi Rafael,

One possibility:

If you have a Control on a Form, eg a hidden Label, and 'call' it from
code running in the Listener's thread using oForm1.oUsefulLabel.BeginInvoke
(YourThreadCallback), it will call YourThreadCallback in the UI thread. You
can then do whatever you like. (The Label is just used for the mechanism).

Regards,
Fergus
 
* "Rafael Pivato said:
My class launches a thread to stay listenning for a event (a serial comm
event).
When I raise the my .Net event, the handler will be running in the context
of this new thread.
In a Windows Forms application, how can I force it to run at the "main"
thread ?

You can use invoking to call a method in the GUI thread:

<http://www.devx.com/dotnet/Article/11358>
 
* "Rafael Pivato said:
Thanks! That's a much better approach!
Feels like programming in Java!

Oh, I hope it _doesn't_ feel like programming in Java...
 
Back
Top