2

我有一些 UI 代码需要从我的后台演示者线程中更新。所以,我做了以下事情:从我的后台线程,我在 UI 中设置我的属性:

_ui.ConnectionStatus = "A";

那么,我的设置如下:

public string ConnectionStatus
{
    set
    {
        if (Dispatcher.CheckAccess())
            ConnectionStatusTxt.Content = value;
        else
        {
            Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
                              {ConnectionStatusTxt.Content = value;}));
        }
    }
}

我收到以下错误:

The calling thread cannot access this object because a different thread owns it.

我的理解是Dispatcher负责调用不同的线程,所以这个错误让我有点困惑。谢谢!

4

2 回答 2

5

Another question: what type is value? is this a string? I could imagine that the error might be that value is in fact a UIElement (maybe a Label?) that you create in which case the exception refers to the value object and not to your user control.

于 2009-02-06T00:49:12.350 回答
0

你没有说这个setter在里面执行什么对象,但是看起来这个对象的Dispatcher与ConnectionStatusTxt的dispatcher不一样,即包含setter的对象由与ConnectionStatusTxt控件不同的线程拥有。

尝试在 CheckAccess 和 Invoke 语句中使用 ConnectionStatusTxt.Dispatcher。

于 2009-02-06T00:27:52.673 回答