How to get a list of running tasks in .net 4.0

I am trying to get a list of all currently running tasks. Does .net 4.0 api provide such functionality? Or the only option is to explicitly store tasks in a separate collection?

+6
source share
4 answers

I suppose you need the TaskScheduler.GetScheduledTasks method, but:

  • He is protected
  • MSDN says it should be used only for debugging
  • As far as I know, this method is implemented only by ThreadPoolTaskScheduler, SynchronizationContextTaskScheduler always returns null

So, I think you should try to implement your own TaskScheduler to achieve your goals.

+4
source

When you create a task, by default the task is scheduled to run in the thread pool thread . Thus, you can get the number of running tasks using ThreadPool.GetMaxThreads and ThreadPool.GetAvailableThreads .

private static int GetWorkingThreads() { int maxThreads; int completionPortThreads; ThreadPool.GetMaxThreads(out maxThreads, out completionPortThreads); int availableThreads; ThreadPool.GetAvailableThreads(out availableThreads, out completionPortThreads); return maxThreads - availableThreads; } 
+2
source

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).

+1
source

There is a good article on MSDN http://msdn.microsoft.com/en-us/library/ms997649.aspx Everything you need is described very well.

Edit: perhabs this will help:

 using System; using System.Diagnostics; class MainClass { public static void Main() { Process[] allProcs = Process.GetProcesses(); foreach(Process proc in allProcs) { ProcessThreadCollection myThreads = proc.Threads; Console.WriteLine("process: {0}, id: {1}", proc.ProcessName, proc.Id); foreach(ProcessThread pt in myThreads) { Console.WriteLine(" thread: {0}", pt.Id); Console.WriteLine(" started: {0}", pt.StartTime.ToString()); Console.WriteLine(" CPU time: {0}", pt.TotalProcessorTime); Console.WriteLine(" priority: {0}", pt.BasePriority); Console.WriteLine(" thread state: {0}", pt.ThreadState.ToString()); } } } } 
0
source

Source: https://habr.com/ru/post/917080/


All Articles