Does the current task scheduler flow into ThreadPool.QueueUserWorkItem callbacks, as a part of the execution context flow?
I expect it would not, so the following debug asserts should be passing. I couldn't find any confirmation of that online.
var scheduler = new CustomTaskScheduler();
await Task.Factory.StartNew(async () =>
{
Debug.Assert(TaskScheduler.Current == scheduler);
var tcs = new TaskCompletionSource<bool>();
ThreadPool.QueueUserWorkItem(_ =>
{
Debug.Assert(TaskScheduler.Current != scheduler);
Debug.Assert(TaskScheduler.Current == TaskScheduler.Default);
tcs.SetResult(true);
});
await tcs.Task;
Debug.Assert(TaskScheduler.Current == scheduler);
},
CancellationToken.None,
TaskCreationOptions.None,
scheduler).Unwrap();