开发者

Calling thread cannot access object because different thread owns it

开发者 https://www.devze.com 2023-03-16 17:20 出处:网络
I thought I knew what causes this exception until I wrote this: var menu = ViewConfigHelper.CreateObjectFromResource<Menu>(config, baseURI);

I thought I knew what causes this exception until I wrote this:

var menu = ViewConfigHelper.CreateObjectFromResource<Menu>(config, baseURI);
if (!menu.Dispa开发者_如何学Pythontcher.CheckAccess())
{
    throw new ArgumentException("Somethign wrong");
}
if (!LayoutRoot.Dispatcher.CheckAccess())
{
    throw new ArgumentException("SOmethign wrong");
}
 // exception throw here
LayoutRoot.Children.Insert(0, menu);

First line creates a Menu control from an embedded XAML file. Both CheckAccess calls return true. However, when last line is executed, an exception is thrown with the message "Caling thread cannot access object because differrent thread owns it." The code above is being executed within a method called immediately after InitializeComponent() that created LayoutRoot, on the same thread, I believe.

Someone please enlighten me. I am trying to create a multiple UI thread WPF app.


You are using CheckAccess() in reverse. You want to lose the ! signs before each check. See the example bit of code on the CheckAccess() MSDN page.

In the Winforms world you'd do a InvokeRequired() which is now the same thing as a !CheckAccess(). In your case because both values are returning true, and you are inverting them, neither if block is hit.

To expand a bit... in the Winforms world, the normal patter was:

if(InvokeRequired)
{
    Invoke(...);
}
else
{
    //do work
}

(or sometimes a return after invoke, if it was invoking the same method).

In WPF, CheckAccess() is similar to, but not identical to InvokeRequired... there for a pattern more along the lines of:

if (someUiControl.Dispatcher.CheckAccess())
{
    //Doing an update from this thread is safe, so we can do so here.
}
else
{
    // This thread does not have access to the UI thread.
    // Call the update thread via a Dispatcher.BeginInvoke() call.
}

The key difference between is that InvokeRequired() returning true meant it was UNSAFE to do the update in the current thread... while a true from CheckAccess() means it is SAFE.

0

精彩评论

暂无评论...
验证码 换一张
取 消