Waiting for ThreadPool

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael D. Ober

    #1

    Waiting for ThreadPool

    If I use standard Threading.Threa d threads, I can issue a Join on the thread
    variable and wait for it to complete. How can I do this on the Background
    Worker Threads in a threadpool without looping. Basically, I will be
    queuing an unknown number of worker threads in the pool and I need to wait
    for them all to complete.

    Thanks,
    Mike Ober.



  • Nick Hounsome

    #2
    Re: Waiting for ThreadPool


    "Michael D. Ober" <obermd.@.alum. mit.edu.nospam> wrote in message
    news:wbC1g.3973 $Fy2.3884@newsr ead3.news.pas.e arthlink.net...[color=blue]
    > If I use standard Threading.Threa d threads, I can issue a Join on the
    > thread
    > variable and wait for it to complete. How can I do this on the Background
    > Worker Threads in a threadpool without looping. Basically, I will be
    > queuing an unknown number of worker threads in the pool and I need to wait
    > for them all to complete.[/color]

    Pool threads never exit or they wouldn't be in the pool. Consequently you
    cannot Join with them.

    See WaitHandle.Wait All

    NB. It is crucial when using pool threads that you don't throw an exception
    out of your delegate without signalling that it has exited (unlike Join
    where you will get to know) therefore the delegate should ALWAYS be a
    try...catch...f inally block.

    Ideally you use some sort of shared object to store the completion state,
    initialize it to "software messed up" at the top of the try block, set it to
    "success" at the bottom, set it to sepcific errors in the catch and signal
    completion in the finally block.


    Comment

    • Michael D. Ober

      #3
      Re: Waiting for ThreadPool


      Thanks.

      Mike.

      "Nick Hounsome" <News@NickHouns ome.Me.Uk> wrote in message
      news:JkG1g.2934 02$zk4.106149@f e3.news.blueyon der.co.uk...[color=blue]
      >
      >
      > "Michael D. Ober" <obermd.@.alum. mit.edu.nospam> wrote in message
      > news:wbC1g.3973 $Fy2.3884@newsr ead3.news.pas.e arthlink.net...[color=green]
      > > If I use standard Threading.Threa d threads, I can issue a Join on the
      > > thread
      > > variable and wait for it to complete. How can I do this on the[/color][/color]
      Background[color=blue][color=green]
      > > Worker Threads in a threadpool without looping. Basically, I will be
      > > queuing an unknown number of worker threads in the pool and I need to[/color][/color]
      wait[color=blue][color=green]
      > > for them all to complete.[/color]
      >
      > Pool threads never exit or they wouldn't be in the pool. Consequently you
      > cannot Join with them.
      >
      > See WaitHandle.Wait All
      >
      > NB. It is crucial when using pool threads that you don't throw an[/color]
      exception[color=blue]
      > out of your delegate without signalling that it has exited (unlike Join
      > where you will get to know) therefore the delegate should ALWAYS be a
      > try...catch...f inally block.
      >
      > Ideally you use some sort of shared object to store the completion state,
      > initialize it to "software messed up" at the top of the try block, set it[/color]
      to[color=blue]
      > "success" at the bottom, set it to sepcific errors in the catch and signal
      > completion in the finally block.
      >
      >
      >[/color]



      Comment

      • William Stacey [MVP]

        #4
        Re: Waiting for ThreadPool

        In the delegate, increment an int before exit. The last delegate out closes
        the door - or in this case, it does a Set(). You other thread is waiting on
        event.WaitOne() similar to a Join. So one event should handle it.

        try
        {
        // run delegate code.
        }
        finally
        {
        int c = Interlocked.Inc rement(ref count);
        if ( c >= numToWait) // Last one out?
        autoEvent.Set() ;
        }

        --
        William Stacey [MVP]

        "Michael D. Ober" <obermd.@.alum. mit.edu.nospam> wrote in message
        news:wbC1g.3973 $Fy2.3884@newsr ead3.news.pas.e arthlink.net...
        | If I use standard Threading.Threa d threads, I can issue a Join on the
        thread
        | variable and wait for it to complete. How can I do this on the Background
        | Worker Threads in a threadpool without looping. Basically, I will be
        | queuing an unknown number of worker threads in the pool and I need to wait
        | for them all to complete.
        |
        | Thanks,
        | Mike Ober.
        |
        |
        |


        Comment

        Working...