问题描述
我正在尝试通过使用接口从我们的视图模型中提取Dispatcher
。我已经创建了该接口的模拟实现,如下所示:
public class MockIDispatcher : IDispatcher,IDisposable
{
public MockIDispatcher()
{
var dispatcherThread = new Thread(Dispatcher.Run) { IsBackground = true };
dispatcherThread.SetApartmentState(ApartmentState.STA);
dispatcherThread.Start();
while ((Dispatcher = Dispatcher.FromThread(dispatcherThread)) == null) Thread.Yield(); // need to wait until the thread we created has started its dispatcher
}
internal Dispatcher Dispatcher { get; }
/* ... more implementation here */
}
我已经创建了一个虚拟的NUnit测试,如下所示,但是对Dispatcher.Invoke()
的调用抛出了TaskCanceledException
。
[Test]
public void TestPoc()
{
var foo = new MockIDispatcher();
foo.Dispatcher.Invoke(() =>
{
Debug.WriteLine("Hey there!");
});
}
有关如何使此代码正常工作的任何建议?我实际上想在幕后使用Dispatcher,以便更轻松地玩SynchronizationContext
之类的东西。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)