About Thread Completion Notify.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • objectref

    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.Threadin g;

    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.WriteLi ne("Main thread is waiting for BOTH tasks to complete.");
    ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Do Task), waitHandles[0]);
    ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Do Task), waitHandles[1]);
    WaitHandle.Wait All(waitHandles );

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

    // Queue up two tasks on two different threads;
    // wait until any tasks are completed.
    dt = DateTime.Now;
    Console.WriteLi ne();
    Console.WriteLi ne("The main thread is waiting for either task to
    complete.");
    ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Do Task), waitHandles[0]);
    ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Do Task), waitHandles[1]);

    int index = WaitHandle.Wait Any(waitHandles );
    // The time shown below should match the shortest task.
    Console.WriteLi ne("Task {0} finished first (time waited={1}).",
    index + 1, (DateTime.Now - dt).TotalMillis econds);
    }

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



    thanks in advance fro any help


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: About Thread Completion Notify.

    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]
    - mvp@spam.guard. caspershouse.co m

    "objectref" <objectref@medi atrel.com> wrote in message
    news:%23WQzMMOc FHA.1392@TK2MSF TNGP14.phx.gbl. ..[color=blue]
    > 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.Threadin g;
    >
    > 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.WriteLi ne("Main thread is waiting for BOTH tasks to complete.");
    > ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Do Task), waitHandles[0]);
    > ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Do Task), waitHandles[1]);
    > WaitHandle.Wait All(waitHandles );
    >
    > // The time shown below should match the longest task.
    > Console.WriteLi ne("Both tasks are completed (time waited={0})",
    > (DateTime.Now - dt).TotalMillis econds);
    >
    > // Queue up two tasks on two different threads;
    > // wait until any tasks are completed.
    > dt = DateTime.Now;
    > Console.WriteLi ne();
    > Console.WriteLi ne("The main thread is waiting for either task to
    > complete.");
    > ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Do Task), waitHandles[0]);
    > ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Do Task), waitHandles[1]);
    >
    > int index = WaitHandle.Wait Any(waitHandles );
    > // The time shown below should match the shortest task.
    > Console.WriteLi ne("Task {0} finished first (time waited={1}).",
    > index + 1, (DateTime.Now - dt).TotalMillis econds);
    > }
    >
    > static void DoTask(Object state) {
    > AutoResetEvent are = (AutoResetEvent ) state;
    > int time = 1000 * r.Next(2, 10);
    > Console.WriteLi ne("Performing a task for {0} milliseconds.", time);
    > Thread.Sleep(ti me);
    > are.Set();
    > }
    > }
    >
    >
    >
    > thanks in advance fro any help
    >[/color]


    Comment

    • objectref

      #3
      Re: About Thread Completion Notify.

      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]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:eiHOzQPcFH A.2664@TK2MSFTN GP15.phx.gbl...[color=blue]
      > 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]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "objectref" <objectref@medi atrel.com> wrote in message
      > news:%23WQzMMOc FHA.1392@TK2MSF TNGP14.phx.gbl. ..[color=green]
      >> 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.Threadin g;
      >>
      >> 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.WriteLi ne("Main thread is waiting for BOTH tasks to complete.");
      >> ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Do Task), waitHandles[0]);
      >> ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Do Task), waitHandles[1]);
      >> WaitHandle.Wait All(waitHandles );
      >>
      >> // The time shown below should match the longest task.
      >> Console.WriteLi ne("Both tasks are completed (time waited={0})",
      >> (DateTime.Now - dt).TotalMillis econds);
      >>
      >> // Queue up two tasks on two different threads;
      >> // wait until any tasks are completed.
      >> dt = DateTime.Now;
      >> Console.WriteLi ne();
      >> Console.WriteLi ne("The main thread is waiting for either task to
      >> complete.");
      >> ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Do Task), waitHandles[0]);
      >> ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Do Task), waitHandles[1]);
      >>
      >> int index = WaitHandle.Wait Any(waitHandles );
      >> // The time shown below should match the shortest task.
      >> Console.WriteLi ne("Task {0} finished first (time waited={1}).",
      >> index + 1, (DateTime.Now - dt).TotalMillis econds);
      >> }
      >>
      >> static void DoTask(Object state) {
      >> AutoResetEvent are = (AutoResetEvent ) state;
      >> int time = 1000 * r.Next(2, 10);
      >> Console.WriteLi ne("Performing a task for {0} milliseconds.", time);
      >> Thread.Sleep(ti me);
      >> are.Set();
      >> }
      >> }
      >>
      >>
      >>
      >> thanks in advance fro any help
      >>[/color]
      >
      >[/color]


      Comment

      Working...