Raising events to run at the "main" thread

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 ?
 
F

Fergus Cooney

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
 
H

Herfried K. Wagner [MVP]

* "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>
 
H

Herfried K. Wagner [MVP]

* "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...
 

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