1

I need it for debugging, through the code and not through some UI tools.

The class TaskScheduler has the property Current which returns ThreadPoolTaskScheduler, but it is internal, apparently in taskschd.dll.

Also GetScheduledTasks() of TaskScheduler is protected In other words, you cannot use it to list the tasks. I need to list all the tasks including those created indirectly inside the base NET code on behalf of my console app

2
  • Have you tried using reflection to access the internals? I'm not advicing this for production, just saying.
    – Silvermind
    Feb 21, 2018 at 18:08
  • TaskScheduler has two internal methods that are used by the debugger, that's how it implements the Debug > Windows > Tasks tool window in VS. The static TaskScheduler[] GetTaskSchedulersForDebugger() lets you iterate all schedulers, Task[] GetScheduledTasksForDebugger lets you iterate all the tasks for each scheduler. Use reflection to call them. Do beware that calling them while the program is running and actively creating/executing tasks is not particularly safe, not an issue with the debugger since it freezes the program. Feb 21, 2018 at 20:06

1 Answer 1

0

You can do it with System.Reflection api. You can query for non-public members by giving proper BindingFlags to reflection methods:

var type = typeof(TaskScheduler);
var fieldinfo = type.GetProperty("Current", BindingFlags.Instance | BindingFlags.NonPublic);
var methodInfo = type.GetMethod("GetScheduledTasks", BindingFlags.Instance | BindingFlags.NonPublic));

..and after getting metadata for property and method, you can call PropertyInfo.GetValue or MethodInfo.Invoke.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.