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

Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
  • Have you tried using reflection to access the internals? I'm not advicing this for production, just saying. – Silvermind Feb 21 '18 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. – Hans Passant Feb 21 '18 at 20:06

1 Answers1

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.

Risto M
  • 2,919
  • 1
  • 14
  • 27