Solved

Find all Tasks I've spawned using Task.Factory.StartNew

Posted on 2010-11-14
2
973 Views
Last Modified: 2013-04-16
In C# (.NET 4.0), I am kicking off a continuous stream of Task instances to asynchronously complete pieces of work, using the Task.Factory.StartNew() method.  In general I don't need to keep track of them or wait for them to complete, since they will finish by themselves.  

I am running this code in a Windows Service.  If an administrator needs to shut down the service, I want to do a graceful shutdown. Is there a way to catch the stop request, and then to find all the Tasks that I've launched that are still executing, so I can wait for them to complete with a Task.WaitAll()?  Some kind of Task.FindBy??? or Task.FindAll method?  
0
Comment
Question by:jblindberg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 500 total points
ID: 34134092
I don't think there's a FindAll if you are looking for it, You would need to maintain your own list of Task classes the Task class has a method WaitAll that takes an array of tasks as argument to wait upon, so if you maintain your own list you can just call this function and pass the list items as argument.

If some of your tasks would be long enough and you would want to cancel them rather than wait on them there's a different pattern look for CancellationTokenSource refer http://www.albahari.com/threading/part5.aspx#_Canceling_Tasks

0
 
LVL 1

Expert Comment

by:rafaelrgl
ID: 39084809
arif_eqbal, can you check my question about the waitall tasks
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question