Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 513
Markus Avatar asked May 31 '12 08:05

Markus


People also ask

How are tasks created in .NET framework?

NET framework provides Threading. Tasks class to let you create tasks and run them asynchronously. A task is an object that represents some work that should be done. The task can tell you if the work is completed and if the operation returns a result, the task gives you the result.

What are tasks in C#?

Task represents an asynchronous operation in C#. The following states how you can start a task in C#. Use a delegate to start a task. Task t = new Task(delegate { PrintMessage(); }); t. Start();

Is a Task a thread?

A task is something you want done. A thread is one of the many possible workers which performs that task. In . NET 4.0 terms, a Task represents an asynchronous operation.

How do I run a parallel Task in C#?

If you have several tasks that can be run in parallel, but still need to wait for all the tasks to end, you can easily achieve this using the Task. WhenAll() method in . NET Core. This will upload the first file, then the next file.


3 Answers

I suppose you need TaskScheduler.GetScheduledTasks method, but:

  1. It's protected
  2. MSDN says it should be used only for debugging
  3. As far as I know this method has implemented only by ThreadPoolTaskScheduler, SynchronizationContextTaskScheduler always returns null

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

like image 112
Rusted Avatar answered Oct 16 '22 21:10

Rusted


Why would you want to find the list of running tasks? Apart from debugging, you shouldn't have to use this information. In any case, there is a difference between the list of the tasks that are scheduled for execution and the tasks that are actually executing.

As Rusted writes, you can get the number of scheduled tasks from the TaskScheduler.GetScheduledTasks method. The method is abstract so it has to be implemented by all TaskSchedulers.

How many tasks actually execute depends on the TaskScheduler implementation. The default task scheduler uses the threadpool, in which case you should check ThreadPool.GetAvailableThreads and ThreadPool.GetMaxThreads to approximate the number of executing tasks.

There is no actual list of running tasks, even when you use the default TaskScheduler. The scheduler essentially assigns a task to a ThreadPool and leaves the actual execution to the pool (actually, it calls the Task.ExecuteEntry private method). It has no need to keep a list of the running tasks.

If you want the running task information for debugging purposes, you can take advantage of the Event Trace for Windows events in TPL. Unfortunately, the Task Started and Task Finished events are not documented. I found them while browsing the definition of Task.ExecuteEntry with dotPeek.

I've found one article exploring the TPL events but I'm not sure it's worth the trouble. Unless you are writing your own debugger, it seems too much trouble.

If you just have to get 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 place each task in a list. This could get expensive though, and you would have to do some cleanup periodically to remove completed tasks from the list without using continuations (which would end up in the list themselves).

like image 21
Panagiotis Kanavos Avatar answered Oct 16 '22 20:10

Panagiotis Kanavos


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

    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;
    }
like image 20
Unril Avatar answered Oct 16 '22 20:10

Unril