Why do you want to find a list of running tasks? Besides debugging, you do not need to use this information. In any case, there is a difference between the list of tasks planned for execution and tasks that are actually being performed.
As Rusted writes, you can get the number of scheduled tasks from the TaskScheduler.GetScheduledTasks method. The method is abstract, so it must be implemented by all TaskSchedulers.
How many tasks actually performed depends on the implementation of TaskScheduler. The Task Scheduler uses threadpool by default, in which case you should check ThreadPool.GetAvailableThreads and ThreadPool.GetMaxThreads to get closer to the number of tasks performed.
There is no actual list of running tasks, even if you use TaskScheduler by default. The scheduler essentially assigns the ThreadPool task and leaves the actual execution in the pool (in fact, it calls the private Task.ExecuteEntry method). He does not need to save a list of running tasks.
If you need information about a running task for debugging, you can use Event Tracing for Windows events in TPL . Unfortunately, events triggered by the launch of a task and tasks are not documented. I found them while looking at the definition of Task.ExecuteEntry using dotPeek.
I found one article investigating TPL events , but I'm not sure if it is worth it. If you do not write your own debugger, this seems too complicated.
If you just need to go to the list of running tasks, perhaps you should write your own TaskScheduler and override TryExecute and TryExecuteInternal to intercept the execution of each task and put each task in the list. This can become costly and you will have to periodically clean up to remove completed tasks from the list without using the continuations (which go to the list themselves).
source share