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

Re: Multi threading apps and raising events by AlexS

AlexS
Thu May 13 13:04:34 CDT 2004

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

"Alok" <alok_sathaye@pune.tcs.co.in> wrote in message
news:%231fpsgPOEHA.308@TK2MSFTNGP11.phx.gbl...
> 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
>
>



Re: Multi threading apps and raising events by Alok

Alok
Thu May 13 23:16:51 CDT 2004

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 :)

"AlexS" <salexru2000NO@SPAMsympaticoPLEASE.ca> wrote in message
news:OETuxuPOEHA.1340@TK2MSFTNGP12.phx.gbl...
> 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
>
> "Alok" <alok_sathaye@pune.tcs.co.in> wrote in message
> news:%231fpsgPOEHA.308@TK2MSFTNGP11.phx.gbl...
> > 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
> >
> >
>
>



Re: Multi threading apps and raising events by AlexS

AlexS
Thu May 13 14:00:02 CDT 2004

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

"Alok" <alok_sathaye@pune.tcs.co.in> wrote in message
news:eMTpAGQOEHA.1644@TK2MSFTNGP09.phx.gbl...
> 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 :)
>
> "AlexS" <salexru2000NO@SPAMsympaticoPLEASE.ca> wrote in message
> news:OETuxuPOEHA.1340@TK2MSFTNGP12.phx.gbl...
> > 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
> >
> > "Alok" <alok_sathaye@pune.tcs.co.in> wrote in message
> > news:%231fpsgPOEHA.308@TK2MSFTNGP11.phx.gbl...
> > > 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
> > >
> > >
> >
> >
>
>



Re: Multi threading apps and raising events by Justin

Justin
Thu May 13 13:24:57 CDT 2004

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).


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


"AlexS" <salexru2000NO@SPAMsympaticoPLEASE.ca> wrote in message
news:u8uNxNQOEHA.2468@TK2MSFTNGP11.phx.gbl...
> 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
>
> "Alok" <alok_sathaye@pune.tcs.co.in> wrote in message
> news:eMTpAGQOEHA.1644@TK2MSFTNGP09.phx.gbl...
> > 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 :)
> >
> > "AlexS" <salexru2000NO@SPAMsympaticoPLEASE.ca> wrote in message
> > news:OETuxuPOEHA.1340@TK2MSFTNGP12.phx.gbl...
> > > 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
> > >
> > > "Alok" <alok_sathaye@pune.tcs.co.in> wrote in message
> > > news:%231fpsgPOEHA.308@TK2MSFTNGP11.phx.gbl...
> > > > 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
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Re: Multi threading apps and raising events by John

John
Thu May 13 19:11:19 CDT 2004

"Alok" <alok_sathaye@pune.tcs.co.in> wrote in message
news:%231fpsgPOEHA.308@TK2MSFTNGP11.phx.gbl...
> 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]

--
John Saunders
John.Saunders at SurfControl.com