WindowsFormsSynchronizationContext.Current.Post在后台线程上执行lambda

时间:2013-07-16 13:18:08

标签: c# multithreading winforms

将lambda表达式发布到当前WindowsFormsSynchronizationContext时,我发现lambda代码在后台线程上执行:

// running on main thread here

myLabel = new Label();
this.Controls.Add(myLabel);

WindowsFormsSynchronizationContext.Current.Post( ignore => {

    // returns true !
    bool invokeRequired = myLabel.InvokeRequired; 

    // returns a background thread, not the UI thread
    int threadId = System.Threading.Thread.CurrentThread.ManagedThreadId; 

    // throws, because we are (unexpectedly) on a background, different thread
    myLabel.Text = "whatever";

},null);

此外,WindowsFormsSynchronizationContext.Current似乎不会返回WindowsFormsSynchronizationContext,而是返回普通的System.Threading.SynchronizationContext

这突然发生在以前没有线程问题且最近没有修改过的表单上(解决方案的其他部分)。我试图寻找明显的错误(例如,在后台线程上实例化表单本身的代码,或者在后台线程上创建的控件),但是我找不到重大的违规行为。

也许我看错了方向?

1 个答案:

答案 0 :(得分:5)

WindowsFormsSynchronizationContext.CurrentSynchronizationContext.Current相同。您应该收到一个编译器警告您正在通过派生类调用基本静态成员。

检查SynchronizationContext.Current.GetType(),您会发现您在不同的同步环境下运行。

在UI线程上捕获正确的上下文并存储它。

相关问题