Track TaskSchedulers only when debugger attached
authorstephentoub <stoub@microsoft.com>
Thu, 11 Feb 2016 18:06:41 +0000 (13:06 -0500)
committerstephentoub <stoub@microsoft.com>
Thu, 11 Feb 2016 18:06:41 +0000 (13:06 -0500)
commitae413bc05a6021c5164cc34d899084bc2a2a0579
treebf53270a9645fa70b3dd5b8d39b108f018649f37
parent4181cfa65a889e98878bf879bd50e6ebcc0118c3
Track TaskSchedulers only when debugger attached

Today the base TaskScheduler ctor registers every created scheduler in a ConditionalWeakTable.  This is then used in the internal TaskScheduler.GetTaskSchedulersForDebugger() to enable a debugger to find all of the schedulers that are active and then in turn find all tasks scheduled to them.  However, for scenarios that involve creating many, many schedulers, this registration adds up to a non-trivial cost, both in time and space, and is only relevant for debugging.  Plus, since this mechanism was introduced, Visual Studio has additional ways of tracking tasks for display in the Tasks window, e.g. ETW events.

This commit simply makes that registration more pay-for-play, only doing it if the debugger is attached at the time of the scheduler's creation.
src/mscorlib/src/System/Threading/Tasks/TaskScheduler.cs
src/mscorlib/src/System/Threading/Tasks/ThreadPoolTaskScheduler.cs