473,394 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.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
2 3441
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Duane Pressley | last post by:
I'm looking for someone to help me make sense of the results I'm observing when using the ThreadPool class in a COM-Interop scenario. Here's the set up: 1.. A classic ASP page instantiates and calls...
3
by: SSonnenwald | last post by:
I have created a piece of code that uses a ThreadPool class and the QueueUserWorkItem method to add items to the the ThreadPool. What would seem to be simple I just can not figure out how to tell...
2
by: fred | last post by:
I'm writing an application invoking asynchon methods. I envision using the .NET thread pool since it matches well my needs. There is (at least) 2 methods to do that: using ThreadPool class...
5
by: Peter Kirk | last post by:
Hi, I see in the ThreadPool documentation that the pool has a default limit of 25 threads. Is it correctly understood that this limit is for my entire application? So if I have several...
5
by: Henri | last post by:
Might sound a stupid question but need to be sure: When you call ThreadPool.QueueUserWorkItem and ThreadPool.RegisterWaitForSingleObject passing a New object as state e.g.:...
4
by: Jonathan Howard | last post by:
I am trying to create a page where multiple requests are made to a single webservice. This webservice takes a fair amount of time to process the requests so rather than making the calls one at a...
1
by: pmclinn | last post by:
In the code format below, is there any way to pass in a variable to the writefiles proceedure? ThreadPool.QueueUserWorkItem(AddressOf WriteFiles) Public Sub WriteFiles(ByVal stateInfo As...
1
by: colinjack | last post by:
Hi All, I've been using the original (non-event based) asynchronous design pattern using delegates. It works fine but performance wise I know using the ThreadPool is supposed to be far better,...
1
by: gnassar | last post by:
This question is prob. extremely trivial but I can't seem to find out what is going on. I've been doing some basic work with Multithreading and Worker Threads and have determined I'd like to set up...
1
by: robert112 | last post by:
Question... Can one not use ThreadPool.QueueUserWorkItem with an anonymous method like so: ThreadPool.QueueUserWorkItem(delegate { //perform IO bound operation. }); Why did the asp.net team...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.