473,785 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ThreadPool.Queu eUserWorkItem

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


Feb 15 '06 #1
2 3502
any advise ?
"James" <jk****@hotmail .com> wrote in message
news:u0******** ******@TK2MSFTN GP14.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.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


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

Feb 16 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2761
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 MethodA on COM+ Class (C1). 2.. MethodA of COM+ class (C1) instantiates and calls MethodB of .NET class (N1). (Note: N1 has been registered using RegASM and is present in the GAC). 3.. MethodB of .NET class (N1) uses the...
3
5230
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 if all of the threads in the threadpool that I queued have completed. Does anyone have a way???? Any help would be appreciated. Thanks, Scott
2
3645
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 directely or using the BeginInvoke () of a delegate. Does anybody knows any advantages from one method to
5
1703
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 worker-threads each using "ThreadPool.QueueUserWorkItem" then the limit is spread over all my worker-threads - so if "worker-thread-1" currently has 20 calls in progress then there are only 5 thread-pool threads available for "worker-thread-2" ?
5
6379
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.: ThreadPool.QueueUserWorkItem(AddressOf MyCallBack, New MyObject(myArg1, myArg2)) the new object MyObject is instantiated just before QueueUserWorkItem is called, and not just before MyCallBack is called, isn't it?
4
2403
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 time I'd like to create a new thread for each call. I tested this in a stand alone application with no problems at all. My problem is that when I try using the System.Threading.Threadpool class inside the aspx page, nothing happens. I add a...
1
747
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 Object) End Sub
1
2031
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, so I wondered if anyone had any examples of the best way to do this. I've obviously had a look myself but haven't gotten very far and although I've implemented a version myself I'm not sure its the best that can be done.
1
1759
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 a minor project that has ThreadingPools. I can't seem to get it to compile whether I follow the example tutorials or I don't. Of course the latter has a higher percentage of failing :) Anyways the line that fails is:
1
4054
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 create the directive async=true way of creating async pages???
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10327
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.