Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 11th, 2008, 08:34 AM
Authorized User
 
Join Date: Aug 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to tusharkale
Default Multithreading C#: Thread State check

I have 10 threads in my application. How do I check the state of all the threads whether they are still running or closed.

I want to specifiy the condition in loop e.g

while ( thread1.IsStillrunning && thread2.IsStillRunning ... )
{
continue;
}

Is there any way to check the status of all the threads in one statement rather than checking for individuals like (thread1.IsStillRunning , thread2.IsStillRunning , thread3..... n so on)

 
Old August 11th, 2008, 09:10 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Off the top of my head I can think of a couple of ways: you might keep an array of threads, and then check them in a simple loop, counting the number that are still running. Or perhaps get the threads to raise an event when they are finished and count the number of events raised.

/- Sam Judson : Wrox Technical Editor -/
 
Old August 11th, 2008, 09:17 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I don't know the answer to this question, but perhaps it's something that the ThreadPool class handles. Otherwise, I'd say you just create an array or list of the threads and check them all.

Now, considering that you have a while loop that is basically waiting for all the threads to complete, there may be a better way to do this.

I don't know the method off hand but there is a method on a thread that will block until the thread is completed. You could set up a regular for loop across all the thread instances, and call this method for each one. Regardless of which thread finishes first or last, the loop will wait until all the threads finish.

For example, for 10 threads (0-9), if the first loop takes the longest, the for loop will wait on i==0 then spin thru all the rest basically instantly after the first is done (because 1-9 are already finished). If the 5th thread finishes last then the loop will step thru 0 to 3 waiting for each of those to finish then wait on 4, then spin thru 5-9. In essence, the loop will check all threads, but wait on the first longest running one and "skip" waiting on the rest. This is how I have seen this type of thing done.

-Peter
compiledthoughts.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help: check the NUM,CAPS,SCROLL key state gmbalaa General .NET 0 August 11th, 2007 07:10 AM
Multithreading Help jenngarcia21 General .NET 0 March 9th, 2007 02:14 PM
multithreading in c# furqanms C# 0 November 21st, 2006 10:43 AM
regarding multithreading Mystic C# 1 July 22nd, 2006 12:44 AM
Session State|View State|Do I have other options? rockon ASP.NET 1.x and 2.0 Application Design 2 October 5th, 2005 07:10 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.