Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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