我想我在TPL中发现了一个严重的错误。我不确定。我花了很多时间挠头,无法理解这种行为。有人能帮忙吗?
我的场景是:
- I create a task that does simple thing. No exceptions etc.
- I register a continuation with ExecuteSynchronously set. It must be on the same thread.
- I start the task on default taskscheduler (ThreadPool). The starting thread proceeds and waits for it.
- The task starts. Passes.
- The continuation starts on the same thread as task (making previous task completed!) and enters endless loop.
- Nothing happens with waiting thread. Does not want to go further. Stuck on wait. I checked in debugger, task is RunToCompletion.
这是我的代码。感谢任何帮助!
// note: using new Task() and then Start() to avoid race condition dangerous
// with TaskContinuationOptions.ExecuteSynchronously flag set on continuation.
var task = new Task(() => { /* just return */ });
task.ContinueWith(
_task => { while (true) { } /* never return */ },
TaskContinuationOptions.ExecuteSynchronously);
task.Start(TaskScheduler.Default);
task.Wait(); // a thread hangs here forever even when EnterEndlessLoop is already called.