Solved

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

Posted on 2010-11-14
2
968 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

785 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