DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Thu, 16 Aug 2007 04:59:44 -0700,    posted on: microsoft.public.dotnet.framework        back       

Thread Index
  1    UL-Tomten
          2    Jon Skeet [C# MVP]


Implementing a Thread.JoinAll(timeout) using managed threading   
I recently found myself in a situation wanting to
Thread.JoinAll(timeout) (i.e. blocking the calling thread until all a
list of threads have finished or the timeout has expired). I wanted to
do this using only managed threading[1], which means Monitors instead
of WaitHandles. I also didn't want to assume the threads were worker
threads in the thread pool. The solution I came up with resembles the
one described in Joseph Albahari's "Threading in C#" E-book[2]. The
problem is, I (and it) relied on

a. locking, Wait()ing and Pulse()ing to control flow
b. decreasing an "number of active threads" counter
c. keeping track of how much of the desired timeout has passed
d. assuming everything is done when the counter reaches 0

Even though it seems to work, it seems rather kludgy for something
that is more or less 1 line of code if you use the thread pool and
WaitAll(). Can any of you think of a neater solution?

1: From the .NET Framework developer's guide: "Monitor objects are
purely managed, fully portable, and might be more efficient in terms
of operating-system resource requirements. WaitHandle objects
represent operating-system waitable objects, are useful for
synchronizing between managed and unmanaged code, and expose some
advanced operating-system features like the ability to wait on many
objects at once."

2: http://www.albahari.com/threading/part3.html (with working example
implementation)
Date:Thu, 16 Aug 2007 04:59:44 -0700   Author:  

Re: Implementing a Thread.JoinAll(timeout) using managed threading   
UL-Tomten  wrote:

> I recently found myself in a situation wanting to
> Thread.JoinAll(timeout) (i.e. blocking the calling thread until all a
> list of threads have finished or the timeout has expired).


Any reason not to use Thread.Join on each thread in turn, specifying a 
timeout based on how long you've still got left? That would seem the 
most obvious solution.

-- 
Jon Skeet - 
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Date:Thu, 16 Aug 2007 19:34:33 +0100   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us