Find all Tasks I've spawned using Task.Factory.StartNew
Posted on 2010-11-14
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?