473,503 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread Finished Notification?

I have a Manager thread that launches a variable number of Worker threads.
When each Worker thread finishes, I want it to notify the Manager thread
that it's done so that the Manager thread can write to a log file or a
database. What's the best way to do this in C#? I'm betting it has a
simple soultion.

I've looked through several examples of multi-threaded programs on the net
and either they don't do what I want or it's just not clicking with me.

Thanks,
Will.
Nov 15 '05 #1
3 23517
Will,

Unfortunately, there is no managed way to do this. In the unmanaged
world, you can just take the thread handle and pass it to
MsgWaitForMultipleObjects, which will let you know when the thread
completes. Unfortunately, there is nothing like that in .NET.

However, you can get around this easily. Give the thread access to a
ManualResetEvent which the thread would call Set on when it is done. Then,
you could pass an array of events to the static WaitAny method on the
WaitHandle class. This will act as a notification for when the thread is
done processing.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Will" <de*@rusmo.com> wrote in message
news:Nv********************@giganews.com...
I have a Manager thread that launches a variable number of Worker threads.
When each Worker thread finishes, I want it to notify the Manager thread
that it's done so that the Manager thread can write to a log file or a
database. What's the best way to do this in C#? I'm betting it has a
simple soultion.

I've looked through several examples of multi-threaded programs on the net
and either they don't do what I want or it's just not clicking with me.

Thanks,
Will.

Nov 15 '05 #2
How about just storing a ref to the manager thread class in each worker
class. When the worker is done, it calls a Finished method on the mgr
instance that does the logging.

--
William Stacey, MVP

"Will" <de*@rusmo.com> wrote in message
news:Nv********************@giganews.com...
I have a Manager thread that launches a variable number of Worker threads.
When each Worker thread finishes, I want it to notify the Manager thread
that it's done so that the Manager thread can write to a log file or a
database. What's the best way to do this in C#? I'm betting it has a
simple soultion.

I've looked through several examples of multi-threaded programs on the net
and either they don't do what I want or it's just not clicking with me.

Thanks,
Will.

Nov 15 '05 #3
Or you can use standard events to do this - in addition to suggestions of
other posters.
declare event in thread parameters object,
set event handler in main thread before passing object to worker thread
when complete - call event delegate if set.

It's ideal for UI notifications. You have only to remember that delegate
passed will be called on worker thread, so you might need to use
Invoke/BeginInvoke.

And one more - MSMQ. Might be a bit "heavy" apporach, however also useful.

HTH
Alex

"Will" <de*@rusmo.com> wrote in message
news:Nv********************@giganews.com...
I have a Manager thread that launches a variable number of Worker threads.
When each Worker thread finishes, I want it to notify the Manager thread
that it's done so that the Manager thread can write to a log file or a
database. What's the best way to do this in C#? I'm betting it has a
simple soultion.

I've looked through several examples of multi-threaded programs on the net
and either they don't do what I want or it's just not clicking with me.

Thanks,
Will.

Nov 15 '05 #4

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

Similar topics

3
12285
by: cnu | last post by:
Hi, I am starting a new threading to do some process. I want to get notified when the thread does it's job and exits. After receiving the notification, I want to refresh the form. I am...
3
3916
by: Kiran | last post by:
Hi, Was wondering if its possible to have a event notification in Thread when the thread is about to start and the thread has just finished doing its work and about to terminate. If its not...
7
2684
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
16
3286
by: droopytoon | last post by:
Hi, I start a new thread (previous one was "thread timing") because I have isolated my problem. It has nothing to do with calling unmanaged C++ code (I removed it in a test application). I...
6
2270
by: Daniel | last post by:
i have an array that i want all threads to be able to READ from concurrently, however, at times i want to UPDATE the array. at which point i want all threads that use it to block when they try to...
6
2337
by: Anders Eriksson | last post by:
Hello! I have a form from which I start an thread running in the background and I want to have an event when the thread is done/ends. I start the thread like this: private Thread prT; // this...
4
2487
by: Bernie Yaeger | last post by:
How can I know, inside an mdi app, that a particular thread is finished processing? I can't, of course, check it with every form that may open (there are more than 100) and the form that opened it...
3
4372
by: =?Utf-8?B?RkpZ?= | last post by:
Hello All, I met a strange behavior with the system threadpool. The worker thread seems to quit in the middle of processing. Here is the code: I could see the step 1 is finished but step 2 is...
12
3520
by: Tomaz Koritnik | last post by:
Is it possible to asynchronously call a method from worker thread so that this method would execute in main-thread? I'm doing some work in worker thread and would like to report progress to main...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.