James wrote:
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. ?
WaitHandle.WaitAll 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.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