Why does the Task.ContinueWith fail to execute in this Unit Test?

前端 未结 2 1174
深忆病人
深忆病人 2020-12-17 19:45

I have come across a problem with a unit test that failed because a TPL Task never executed its ContinueWith(x, TaskScheduler.FromCurrentSynchronizationContext())

2条回答
  •  独厮守ぢ
    2020-12-17 19:56

    With the line commented out, your SynchronizationContext is the default one you created. This will cause TaskScheduler.FromCurrentSynchrozisationContext() to use the default scheduler, which will run the continuation on the thread pool.

    Once you create a Winforms object like your Form, the current SynchronizationContext becomes a WindowsFormsSynchronizationContext, which in turn will return a scheduler that depends on the WinForms message pump to schedule the continuation.

    Since there is no WinForms pump in a unit test, the continuation never gets run.

提交回复
热议问题