Solved

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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 video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now