Notification from worker thread to Main thread

G

Guest

The are 2 threads - main thread and worker thread. The main thread
creates the worker thread and then the worker thread runs in an infinite
while loop till it is asked to exit by setting an event. The worker thread
instantiates an object and calls methods of that object. If some method call
fails, the main thread needs to be notified about the failure. What
mechanisms exist for notifying the main thread about the worker thread's
status - for example - 'a method call failed'?
 
N

Nicholas Paldino [.NET/C# MVP]

Tim,

The only way to do things like this is to have the thread running in a
loop of some sort, and then set the state that is checked in each iteration
of the loop to see if something happened. This is what is acheived in
Windows Forms, when a call has to be made to the UI thread (through windows
messages).

Hope this helps.
 
G

Guest

Hi Nicholas,

Is there some mechanism like callback that can be implemented, so that
when the secondary thread detects some kind of failure (when it makes its
method calls), it calls back a method running on the primary thread. That
callback method could set an event (Synchronization object) asking the
secondary thread to exit or perform other actions.

Can events and Delegates be used for the same? I am not sure if they can be
used in this scenario.


Nicholas Paldino said:
Tim,

The only way to do things like this is to have the thread running in a
loop of some sort, and then set the state that is checked in each iteration
of the loop to see if something happened. This is what is acheived in
Windows Forms, when a call has to be made to the UI thread (through windows
messages).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tim said:
The are 2 threads - main thread and worker thread. The main thread
creates the worker thread and then the worker thread runs in an infinite
while loop till it is asked to exit by setting an event. The worker thread
instantiates an object and calls methods of that object. If some method
call
fails, the main thread needs to be notified about the failure. What
mechanisms exist for notifying the main thread about the worker thread's
status - for example - 'a method call failed'?
 

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