Multi threading apps and raising events

A

AlexS

Hi Alok

as exception is thrown in worker thread it won't reach main thread. You can
catch it however and invoke event handler, which you define when creating
thread. Once again - delegate will execute in the context of worker thread
so you will need to invoke method on main thread.

Documentation is a bit misleading in this respect - I remember I've seen
something like this. It was referring to rasing event on main thread but did
not specify in details that this is not true for multithreaded situations.

HTH
Alex
 
J

Justin Rogers

Going with Alex here, you can't specify which thread an event happens on.
Instead
you somehow marshall your execution path back to the thread you want to use.
The
WinForms guys do this using a custom windows message that gets read by the UI
thread. That is how they marshal execution back. In your case, you'll need
some
global storage device, perhaps a queue, that each worker thread can spit events
to.

Your main thread, where you need to do the processing, will be responsible for
reading
from that queue. Simple usage of the lock statement to protect access to the
queue, or
through the creation of a synchronized queue wrapper is probably all you really
need.
Toss this new queue onto a static property somewhere and everyone will be able
to
quickly and easily raise events that will later be read off by the main thread
in some
processing loop (note you'll still need to check this queue on the main thread).
 
A

AlexS

Hi, Alok

Form.Invoke allows to synchronize access to controls on form. You have class
with some members, right? If main thread and workers are updating these
members you need to synchronize access then. You can use volatile in
declaration or lock statement. If members are simple enough and you use
static methods - take a look at Thread.Interlocked class and related
methods.

Maybe this can help you to see what you should do
ms-help://MS.NETFrameworkSDKv1.1/cpgenref/html/cpconthreadingdesignguideline
s.htm
- it's MSDN help link

HTH
Alex
 
J

John Saunders

Alok said:
Hi,
I have a multi threaded windows app.
I have the main thread that spawns a thread pool to download multiple files.
In case one of the worker thread throws an exception, how is it propogated
?

It is not propagated at all. You will want to surround your code with a
try-catch block:

private void ThreadMethod()
{
try
{
// do something
}
catch (Exception ex)
{
OnMyEvent(EventArgs.Empty); // Raise your event
}
}
i.e. would the exception handler be called on the worker thread or on the
main thread ?

Any exception handlers will be called on the worker thread.
Secondly, if the worker thread needs to raise an event how should it do it
??

There's only one way to raise an event. What do you mean?

public event EventHandler MyEvent;
protected virtual void OnMyEvent(EventArgs e)
{
if (MyEvent != null)
{
MyEvent(this, e);
}
}

[I'm too lazy to write the code to do a custom event, sorry]
 
A

Alok

Hi,
I have a multi threaded windows app.
I have the main thread that spawns a thread pool to download multiple files.
In case one of the worker thread throws an exception, how is it propogated ?
i.e. would the exception handler be called on the worker thread or on the
main thread ?

Secondly, if the worker thread needs to raise an event how should it do it
?? Should it invoke a method on the main thread or directly raise the event
(reason for asking this is because the documentation says that events are
raised on the working thread)

Thanks in Advance
 
A

Alok

Hi alex,
Thanx a lot for the same.
But my problem still remains i.e. if the delegate is executed on the worker
thread, how do i force it to execute on the main thread. i don't want to
synchronize objects and all.
Also, since all this is happening in a class, i don't have form.invoke()

TIA :)
 

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