473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

threading question

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
Nov 16 '05 #1
3 1963
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

Jon Pope wrote:
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

Nov 16 '05 #2
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

"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
If you are using (or there is a possibility that you will be using) the
CallingClass as part of a user interface, then when you want the worker to
'fire an event' that will tell the calling class that the worker is
finished,
you should really transfer control back to the thread that the calling
class
is running in, to ensure this happens, you need to use a delegate which
you
invoke by calling the BeginInvoke method of the calling class (you might
need
to derive CallingClass from something in order to do this). It's probably
a
good idea to do this anyway, as it enables you to be sure that the
boundary
between classes is the same as the boundary between threads, and you
haven't
got a thread 'calling into' another thread's class.

"Jon Pope" wrote:
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

Nov 16 '05 #3
Not quite, nearly though.
inline

"Jon Pope" wrote:
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.
Jon

"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
If you are using (or there is a possibility that you will be using) the
CallingClass as part of a user interface, then when you want the worker to
'fire an event' that will tell the calling class that the worker is
finished,
you should really transfer control back to the thread that the calling
class
is running in, to ensure this happens, you need to use a delegate which
you
invoke by calling the BeginInvoke method of the calling class (you might
need
to derive CallingClass from something in order to do this). It's probably
a
good idea to do this anyway, as it enables you to be sure that the
boundary
between classes is the same as the boundary between threads, and you
haven't
got a thread 'calling into' another thread's class.

"Jon Pope" wrote:
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


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
19
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException....
3
by: David Harrison | last post by:
I am working on an application on Mac OS X that calls out to python via PyImport_ImportModule(). I find that if the imported module creates and starts a python thread, the thread seems to be...
4
by: Antal Rutz | last post by:
Hi, All! I'm new to threading. I have some design questions: Task: I collect data and store them in an RDBMS (mysql or pgsql) The question is how to do that with threading? The...
6
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service...
7
by: Anthony Nystrom | last post by:
What is the correct way to stop a thread? abort? sleep? Will it start up again... Just curious... If the thread is enabling a form, if the form is disposed is the thread as well? Thanks, ...
4
by: Bob | last post by:
- For cleanup, is it sufficient to set a Thread to Nothing after it's done? - It is OK to pass objects out of the thread? (dumb question maybe but I want to be sure) - What's the best way to...
4
by: DBC User | last post by:
I have a background process which reads a table to see if there are any pending requests. If there are any, then it will start a worker thread (only 10 allowed at a time) and executes a method. In...
4
by: Steven | last post by:
I am taking an "advanced" VB.Net course via web at a state university toward an information science degree. This is my second VB class and I am kind of disappointed in it. This week we covered...
19
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.