为什么SynchronizationContext的工作不正常? [英] Why SynchronizationContext does not work properly?

查看:110
本文介绍了为什么SynchronizationContext的工作不正常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code:

[TestMethod]
public void StartWorkInFirstThread()
{
    if (SynchronizationContext.Current == null)
        SynchronizationContext.SetSynchronizationContext(
            new SynchronizationContext());

    var syncContext = SynchronizationContext.Current;

    Console.WriteLine("Start work in the first thread ({0})", 
        Thread.CurrentThread.ManagedThreadId);

    var action = ((Action) DoSomethingInSecondThread);
    action.BeginInvoke(CallbackInSecondThread, syncContext);

    // Continue its own work
}

private static void DoSomethingInSecondThread()
{
    Console.WriteLine("Do something in the second thread ({0})", 
        Thread.CurrentThread.ManagedThreadId);   
}

private void CallbackInSecondThread(IAsyncResult ar)
{
    Console.WriteLine("Callback in the second thread ({0})", 
        Thread.CurrentThread.ManagedThreadId);
    var syncContext = (SynchronizationContext) ar.AsyncState;
    syncContext.Post(CallbackInFirstThread, null);
}

private void CallbackInFirstThread(object obj)
{
    Console.WriteLine("Callback in the first thread ({0})",
        Thread.CurrentThread.ManagedThreadId);
}

我希望最后选择的第一个线程来执行,即在哪里的SynchronizationContext是从,因为我调用此背景下发布()方法初始线程。即是这样的:

I expect last method to be executed in the first thread, i.e. initial thread where SynchronizationContext is taken from, because I call Post() method of this context. I.e. something like this:

Start work in the first thread (28)
Do something in the second thread (17)
Callback in the second thread (17)
Callback in the first thread (28)

这难道不是的SynchronizationContext的意义?但实际上,我有以下的输出:

Isn't it the meaning of SynchronizationContext? But actually I have following output:

Start work in the first thread (28)
Do something in the second thread (17)
Callback in the second thread (17)
Callback in the first thread (7)

这是什么问题?难道事情出差错的SynchronizationContext或我有一些误会?

更新:我把这种方法,使用ReSharper的测试运行单元测试

Update: I call this method as a unit test using Resharper test runner.

推荐答案

请参阅<一href="http://www.$c$cproject.com/KB/threads/SynchronizationContext.aspx">http://www.$c$cproject.com/KB/threads/SynchronizationContext.aspx

有你需要的答案。你必须重写的SynchronizationContext ,使其正确地处理您的操作。

There is the answer you need. You must override SynchronizationContext to make it properly handling your operations.

读取启动:

请注意,DoWork的执行上   螺纹11,同一个线程RUN1。   没有太大的的SynchronizationContext的   进入主线程。为什么?什么   怎么回事?嗯......这是部分   当你意识到什么是   自由的生活。线程不能只是   在它们之间切换上下文,它们   必须有一个基础设施的内置   到他们在为了做到这一点。用户界面   螺纹,例如,使用消息   泵,并在其   SynchronizationContext的,它利用   消息泵同步到用户界面   主题。

Notice that DoWork is executed on thread 11, the same thread as Run1. Not much of a SynchronizationContext into the main thread. Why? What's going on? Well... This is the part when you realize that nothing is for free in life. Threads can't just switch contexts between them, they must have an infrastructure built-in into them in order to do so. The UI thread, for example, uses a message pump, and within its SynchronizationContext, it leverages the message pump to sync into the UI thread.

这篇关于为什么SynchronizationContext的工作不正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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