threading question

J

Jon Pope

I've got a basic question about threading which I'm hoping someone here will
be able to answer:

Let's say my calling CallingClass has instantiated and started a worker
thread. When my worker thread has completed, it fires an event which
CallingClass has an event handler listening for.

My question is this: in which thread is that event handler operating? The
worker's or CallingClass's? The issue I'm running into is that if I include
a Worker.Join() call within the event handler on CallingClass, it just sits
there forever waiting for the worker thread to stop.

Is this even the proper to signal the end of a thread? I have some
processing that is waiting for the thread is completely finish and shutdown,
and I need a way to find that out. Any help someone can provide would be
greatly appreciated.

Cheers, Jon
 
S

Sijin Joseph

The event handler is ruiing on the worker's thread, you can see this by
placing a breakpoint in the event handlers and then looking at the
Threads window to see the thread that is active. Or you can use the
ThreadID's to check which thread is active, just dump the ThreadID's to
the console.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
 
J

Jon Pope

Thanks, Patty, I appreciate your help. Just so I'm clear: what you're
suggesting is that I pass in a delegate to my Worker thread which points to
a subroutine within my CallingClass. This subroutine is what I want to call
from within my thread when it's done. Is this accurate?

Jon
 
G

Guest

Not quite, nearly though.
inline

Jon Pope said:
Thanks, Patty, I appreciate your help. Just so I'm clear: what you're
suggesting is that I pass in a delegate

When I do it, I don't pass the delegate in. Rather, I define it outside of
both classes (or within one of them if you want, it makes it like a nested
class) but anyway it doesn't need to be a class member. A delegate is a thing
in its own right - where you define it depends on how you want to organise
your project conceptually.

to my Worker thread which points to
a subroutine within my CallingClass. This subroutine is what I want to call
from within my thread when it's done. Is this accurate?

It only points to the method once it's instantiated. Don't forget delegates
have a type definition and an instantiation just like classes. IMHO, they
should be instantiated on one line of code, which also tells them (for the
first and only time) what method they point to, and invoked on the next,
thereafter, they're forgotten and the instance of the delegate goes out of
scope.
Remember it's completely fine for the worker thread to hold a variable
reference to the main form which is operating in another (the primary)
thread, as long as the only method it calls on it is BeginInvoke, in order to
transfer control back to the primary 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