ThreadPool.QueueUserWorkItem

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

    #1

    ThreadPool.QueueUserWorkItem

    i uses the above to send some thread to many machines and perform a ping and
    return results to my event log

    Is there a way to determine when the *last* thread returns, and i can
    terminate my console program automatically. ?

    If i terminate my program *early*, the thread that return results will not
    be written to my event log. Thus i cannot terminate my console program early
    and i have to manually write a loop and expects user to terminate it. My
    loop was :

    Console.WriteLi ne("Press 1 to quit the program or wait for 5 minutes before
    typing 1.")

    While Console.Read() <> 1

    'waiting for my threads to return

    End While






  • James

    #2
    Re: ThreadPool.Queu eUserWorkItem

    any advise ?


    "James" <jkklim@hotmail .com> wrote in message
    news:u0d8QcfMGH A.2696@TK2MSFTN GP14.phx.gbl...[color=blue]
    >i uses the above to send some thread to many machines and perform a ping
    >and return results to my event log
    >
    > Is there a way to determine when the *last* thread returns, and i can
    > terminate my console program automatically. ?
    >
    > If i terminate my program *early*, the thread that return results will not
    > be written to my event log. Thus i cannot terminate my console program
    > early and i have to manually write a loop and expects user to terminate
    > it. My loop was :
    >
    > Console.WriteLi ne("Press 1 to quit the program or wait for 5 minutes
    > before typing 1.")
    >
    > While Console.Read() <> 1
    >
    > 'waiting for my threads to return
    >
    > End While
    >
    >
    >
    >
    >
    >[/color]


    Comment

    • Larry Lard

      #3
      Re: ThreadPool.Queu eUserWorkItem


      James wrote:[color=blue]
      > i uses the above to send some thread to many machines and perform a ping and
      > return results to my event log
      >
      > Is there a way to determine when the *last* thread returns, and i can
      > terminate my console program automatically. ?[/color]

      WaitHandle.Wait All is probably what you are after. Because I am feeling
      generous, I append a VB.NET translation of the WaitAny / WaitAll
      example from one of Jon Skeet's excellent C# threading pages at
      <http://yoda.arachsys.c om/csharp/threads/waithandles.sht ml>. You should
      be able to adapt this example to your own code easily enough.

      Imports System
      Imports System.Threadin g

      Public Class Test

      <MTAThread()> _
      Public Shared Sub Main()
      Dim events As ManualResetEven t() = New ManualResetEven t(10) {}

      Dim i As Integer
      For i = 0 To events.Length - 1
      events(i) = New ManualResetEven t(False)
      Dim r As Runner = New Runner(events(i ), i)
      Dim t As Thread = New Thread(New ThreadStart(Add ressOf
      r.Run))
      t.Start()
      Next

      Dim index As Integer = WaitHandle.Wait Any(events)

      Console.WriteLi ne("***** The winner is {0} *****", index)

      WaitHandle.Wait All(events)
      Console.WriteLi ne("All finished!")
      Console.ReadLin e()
      End Sub

      End Class

      Public Class Runner

      Private Shared ReadOnly rngLock As Object = New Object
      Private Shared rng As Random = New Random

      Private ev As ManualResetEven t
      Private id As Integer

      Friend Sub New(ByVal ev As ManualResetEven t, ByVal id As Integer)
      Me.ev = ev
      Me.id = id
      End Sub

      Friend Sub Run()
      Dim i As Integer

      For i = 0 To 9
      Dim sleepTime As Integer
      ' Not sure about the thread safety of Random...
      SyncLock (rngLock)
      sleepTime = rng.Next(2000)
      End SyncLock

      Thread.Sleep(sl eepTime)

      Console.WriteLi ne("Runner {0} at stage {1}", id, i)
      Next i

      ev.Set()
      End Sub

      End Class

      --
      Larry Lard
      Replies to group please

      Comment

      Working...