Raising event to a different thread

G

Guest

Hi,
I am relatively new at threads. I have a windows form and a class running
on the same thread. The class has a background task that needs to be done
every minute on a background thread. I am still deciding on whether to use
System.Threading.Timer or Thread.Start/Thread.Sleep. The background task may
then raise an event which will be captured by the windows form. I believe
this event needs to be raised on the windows form thread. How do I go about
achieving this?
 
C

Cor Ligthert

Jrh,

When you want to use threading, ask yourself first why do I want to use
threading.
Be aware that it cost you at least more processing time.

When it are processes that take less than let say 3 seconds (and that is
terrible long for a modern computer), than it is already direct senseless to
use threading.

I hope this gives an idea.

Cor
 
A

Armin Zingler

jrh said:
Hi,
I am relatively new at threads. I have a windows form and a class
running on the same thread. The class has a background task that
needs to be done every minute on a background thread. I am still
deciding on whether to use System.Threading.Timer or
Thread.Start/Thread.Sleep. The background task may then raise an
event which will be captured by the windows form. I believe this
event needs to be raised on the windows form thread. How do I go
about achieving this?

In the event handler, use the Form's Invoke/BeginInvoke method to marshal
the call to the other thread.

Armin
 
G

Guest

Thanks for the reply. I'm developing in layers i.e. User Interface layer,
Business Object Layer and Data layer. The task that I want to perform is
part of a business object. I would like to encapsulate it so the business
object event is raised on its thread (which is the same as the UI) instead of
the background thread. Therefore
the UI developer does not have to worry about calling Invoke. Also the UI
developer may not even be aware that threads are being used.

An idea I have is to add a private control to the business object. Then I
could use invoke to then raise the event on the business objects original
thread.
Will this work? Is there another method of marshalling a call to another
thread?
 

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