当前的SynchronizationContext可能不被用作的TaskScheduler [英] The current SynchronizationContext may not be used as a TaskScheduler

查看:1947
本文介绍了当前的SynchronizationContext可能不被用作的TaskScheduler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用任务来运行长时间运行的服务器调用在我的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

The current SynchronizationContext may not be used as a 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆