|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
ThreadPool.QueueUserWorkItemreturn 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.WriteLine("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 any advise ?
Show quoteHide quote "James" <jkk***@hotmail.com> wrote in message news:u0d8QcfMGHA.2696@TK2MSFTNGP14.phx.gbl... >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.WriteLine("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 wrote:
> i uses the above to send some thread to many machines and perform a ping and WaitHandle.WaitAll is probably what you are after. Because I am feeling> 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. ? 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.com/csharp/threads/waithandles.shtml>. You should be able to adapt this example to your own code easily enough. Imports System Imports System.Threading Public Class Test <MTAThread()> _ Public Shared Sub Main() Dim events As ManualResetEvent() = New ManualResetEvent(10) {} Dim i As Integer For i = 0 To events.Length - 1 events(i) = New ManualResetEvent(False) Dim r As Runner = New Runner(events(i), i) Dim t As Thread = New Thread(New ThreadStart(AddressOf r.Run)) t.Start() Next Dim index As Integer = WaitHandle.WaitAny(events) Console.WriteLine("***** The winner is {0} *****", index) WaitHandle.WaitAll(events) Console.WriteLine("All finished!") Console.ReadLine() 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 ManualResetEvent Private id As Integer Friend Sub New(ByVal ev As ManualResetEvent, 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(sleepTime) Console.WriteLine("Runner {0} at stage {1}", id, i) Next i ev.Set() End Sub End Class -- Larry Lard Replies to group please |
|||||||||||||||||||||||