473,418 Members | 1,933 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,418 software developers and data experts.

About Thread Completion Notify.

hi to all folks,
i want to create the following senario:
i have a T1 (Thread 1) that acts like http server that receives incoming
requests.
From that T1, i want to spawn from T1 to Tn thread jobs and beeing able to
be
notified when each of these threads finished it's job.

I would like to implement it with the functionality of the ThreadPool,
i also looked at WaitHandle, WaitAny etc etc.

I also noticed from the msdn example that if i use the AutoResetEvent.Set()
method, i just set the state of the event to signaled but i see no way of
quering this and find it's state so i can understand that it is completed
, and the WaitAll and WaitAny stuff just blocks my T1 so all or any Tn
finishes it's job.

But how i can automate this procedure and each thread notify the main T1 of
the completion of it's job ??
the following is from the msdn help:
using System;
using System.Threading;

public sealed class App {
// Define an array with two AutoResetEvent WaitHandles.
static WaitHandle[] waitHandles = new WaitHandle[] {
new AutoResetEvent(false),
new AutoResetEvent(false)
};

// Define a random number generator for testing.
static Random r = new Random();

static void Main() {
AutoResetEvent are = new AutoResetEvent(false);
are.Set
// Queue up two tasks on two different threads;
// wait until all tasks are completed.
DateTime dt = DateTime.Now;
Console.WriteLine("Main thread is waiting for BOTH tasks to complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
WaitHandle.WaitAll(waitHandles);

// The time shown below should match the longest task.
Console.WriteLine("Both tasks are completed (time waited={0})",
(DateTime.Now - dt).TotalMilliseconds);

// Queue up two tasks on two different threads;
// wait until any tasks are completed.
dt = DateTime.Now;
Console.WriteLine();
Console.WriteLine("The main thread is waiting for either task to
complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);

int index = WaitHandle.WaitAny(waitHandles);
// The time shown below should match the shortest task.
Console.WriteLine("Task {0} finished first (time waited={1}).",
index + 1, (DateTime.Now - dt).TotalMilliseconds);
}

static void DoTask(Object state) {
AutoResetEvent are = (AutoResetEvent) state;
int time = 1000 * r.Next(2, 10);
Console.WriteLine("Performing a task for {0} milliseconds.", time);
Thread.Sleep(time);
are.Set();
}
}

thanks in advance fro any help
Nov 17 '05 #1
2 4685
objectref,

Instead of using events and whatnot, why not just have the DoWork method
call a function at the end to indicate that the task is done, and then
perform whatever work you need to do at that point? Unless you have a
thread somewhere else waiting on the fact that these tasks are done (which
you probably don't since you are servicing requests in separate threads
anyways), you shouldn't have a need for waiting on events.

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

"objectref" <ob*******@mediatrel.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
hi to all folks,
i want to create the following senario:
i have a T1 (Thread 1) that acts like http server that receives incoming
requests.
From that T1, i want to spawn from T1 to Tn thread jobs and beeing able to
be
notified when each of these threads finished it's job.

I would like to implement it with the functionality of the ThreadPool,
i also looked at WaitHandle, WaitAny etc etc.

I also noticed from the msdn example that if i use the
AutoResetEvent.Set()
method, i just set the state of the event to signaled but i see no way of
quering this and find it's state so i can understand that it is completed
, and the WaitAll and WaitAny stuff just blocks my T1 so all or any Tn
finishes it's job.

But how i can automate this procedure and each thread notify the main T1
of
the completion of it's job ??
the following is from the msdn help:
using System;
using System.Threading;

public sealed class App {
// Define an array with two AutoResetEvent WaitHandles.
static WaitHandle[] waitHandles = new WaitHandle[] {
new AutoResetEvent(false),
new AutoResetEvent(false)
};

// Define a random number generator for testing.
static Random r = new Random();

static void Main() {
AutoResetEvent are = new AutoResetEvent(false);
are.Set
// Queue up two tasks on two different threads;
// wait until all tasks are completed.
DateTime dt = DateTime.Now;
Console.WriteLine("Main thread is waiting for BOTH tasks to complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
WaitHandle.WaitAll(waitHandles);

// The time shown below should match the longest task.
Console.WriteLine("Both tasks are completed (time waited={0})",
(DateTime.Now - dt).TotalMilliseconds);

// Queue up two tasks on two different threads;
// wait until any tasks are completed.
dt = DateTime.Now;
Console.WriteLine();
Console.WriteLine("The main thread is waiting for either task to
complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);

int index = WaitHandle.WaitAny(waitHandles);
// The time shown below should match the shortest task.
Console.WriteLine("Task {0} finished first (time waited={1}).",
index + 1, (DateTime.Now - dt).TotalMilliseconds);
}

static void DoTask(Object state) {
AutoResetEvent are = (AutoResetEvent) state;
int time = 1000 * r.Next(2, 10);
Console.WriteLine("Performing a task for {0} milliseconds.", time);
Thread.Sleep(time);
are.Set();
}
}

thanks in advance fro any help

Nov 17 '05 #2
Nicholas,

thanks for the reply.
The reason i cannot do what you suggest me, is that i want to keep track
from the main
thread of all the threads that i spawn, so to be able to stop a thread that
it stalls.
This is a runtime system i have build and there is a possibility for a give
request to
stall, that is, come into an endless loop and the Tn will be live forever.

So, i have to keep track of them and i want to identify and stop a nasty
thread.
This is the reason that i must keep track of the threads and they have to
notify, somehow,
the T1 for their completion status.

Imagine that every thread that i spawn, belongs to an array and when
it finish it's job, i remove it from the array.
Threads that remain in the array for a specified time, are consinered
"nasty" and
will be stop and removed from there...

Any thoughts on this ?

thanks again!


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ei**************@TK2MSFTNGP15.phx.gbl...
objectref,

Instead of using events and whatnot, why not just have the DoWork
method call a function at the end to indicate that the task is done, and
then perform whatever work you need to do at that point? Unless you have
a thread somewhere else waiting on the fact that these tasks are done
(which you probably don't since you are servicing requests in separate
threads anyways), you shouldn't have a need for waiting on events.

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

"objectref" <ob*******@mediatrel.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
hi to all folks,
i want to create the following senario:
i have a T1 (Thread 1) that acts like http server that receives incoming
requests.
From that T1, i want to spawn from T1 to Tn thread jobs and beeing able
to be
notified when each of these threads finished it's job.

I would like to implement it with the functionality of the ThreadPool,
i also looked at WaitHandle, WaitAny etc etc.

I also noticed from the msdn example that if i use the
AutoResetEvent.Set()
method, i just set the state of the event to signaled but i see no way of
quering this and find it's state so i can understand that it is completed
, and the WaitAll and WaitAny stuff just blocks my T1 so all or any Tn
finishes it's job.

But how i can automate this procedure and each thread notify the main T1
of
the completion of it's job ??
the following is from the msdn help:
using System;
using System.Threading;

public sealed class App {
// Define an array with two AutoResetEvent WaitHandles.
static WaitHandle[] waitHandles = new WaitHandle[] {
new AutoResetEvent(false),
new AutoResetEvent(false)
};

// Define a random number generator for testing.
static Random r = new Random();

static void Main() {
AutoResetEvent are = new AutoResetEvent(false);
are.Set
// Queue up two tasks on two different threads;
// wait until all tasks are completed.
DateTime dt = DateTime.Now;
Console.WriteLine("Main thread is waiting for BOTH tasks to complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
WaitHandle.WaitAll(waitHandles);

// The time shown below should match the longest task.
Console.WriteLine("Both tasks are completed (time waited={0})",
(DateTime.Now - dt).TotalMilliseconds);

// Queue up two tasks on two different threads;
// wait until any tasks are completed.
dt = DateTime.Now;
Console.WriteLine();
Console.WriteLine("The main thread is waiting for either task to
complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);

int index = WaitHandle.WaitAny(waitHandles);
// The time shown below should match the shortest task.
Console.WriteLine("Task {0} finished first (time waited={1}).",
index + 1, (DateTime.Now - dt).TotalMilliseconds);
}

static void DoTask(Object state) {
AutoResetEvent are = (AutoResetEvent) state;
int time = 1000 * r.Next(2, 10);
Console.WriteLine("Performing a task for {0} milliseconds.", time);
Thread.Sleep(time);
are.Set();
}
}

thanks in advance fro any help


Nov 17 '05 #3

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

Similar topics

15
by: Valkyrie | last post by:
Refering to the following codes I found, there is nothing displayed in the console, may I ask why? def thrd(param): # the thread worker function print "Received",param import thread for i in...
1
by: Alec Wysoker | last post by:
I have several programs that have two or more threads. The threads that are not the main thread are daemon threads, i.e. the fact that they are running should not stop the program from terminating...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
7
by: VMI | last post by:
In the following code, how can I display the MessageBox once MyThread has finished executing? updateFiles method copies 5 humongous files to the HDD but for some reason, the MessageBox is displayed...
10
by: Teis Draiby | last post by:
In an application manipulating streaming video and 3D stuff I want to implement mutithreading to ensure a decent UI response time. Since my application is very speed critical I want to use the...
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
2
by: gates.liu | last post by:
Sir: I have used FileSystemWathcher(.NET class) to monitor file change envent.The code as list: ...................... int i=0; //Counter for Changed event after FileCreated event private...
11
by: HairlipDog58 | last post by:
Hello, There are several 'cross-thread operation not valid' exception postings in the MSDN discussion groups, but none address my exact situation. I have authored a .NET component in Visual C#...
2
by: sethwai | last post by:
Hi, I have a nightly script that executes inplace reorgs allow write access for several tables after a previous script does a large number of delete operations. It usually has been executing...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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
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...

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.