Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-11-14
2
Medium Priority
?
976 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 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

715 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