By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,694 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,694 IT Pros & Developers. It's quick & easy.

ThreadPool.QueueUserWorkItem

P: n/a
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


Feb 15 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
any advise ?
"James" <jk****@hotmail.com> wrote in message
news:u0**************@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


Feb 16 '06 #2

P: n/a

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

Feb 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.