问题描述
我使用来运行长时间运行的服务器调用在我的ViewModel其结果是在调度
使用 TaskScheduler.FromSyncronizationContext()
编组回来。例如:
I am using Tasks to run long running server calls in my ViewModel and the results are marshalled back on Dispatcher
using TaskScheduler.FromSyncronizationContext()
. For example:
var context = TaskScheduler.FromCurrentSynchronizationContext();
this.Message = "Loading...";
Task task = Task.Factory.StartNew(() => { ... })
.ContinueWith(x => this.Message = "Completed"
, context);
当我执行应用程序能正常工作。但是,当我在 ReSharper的
运行我的 NUnit的
测试中,我得到了调用错误消息 FromCurrentSynchronizationContext
为:
This works fine when I execute the application. But when I run my NUnit
tests on Resharper
I get the error message on the call to FromCurrentSynchronizationContext
as:
电流的SynchronizationContext可能不被用作的TaskScheduler
我想这是因为测试是在工作线程运行。我怎样才能确保测试是在主线程中运行?任何其他建议都欢迎。
I guess this is because the tests are run on worker threads. How can I ensure the tests are run on main thread ? Any other suggestions are welcome.
推荐答案
您需要提供的SynchronizationContext。这是我如何处理它:
You need to provide a SynchronizationContext. This is how I handle it:
[SetUp]
public void TestSetUp()
{
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
}
这篇关于当前的SynchronizationContext可能不被用作的TaskScheduler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!