Thread cannot access the object

社会主义新天地 提交于 2019-12-04 04:39:59

问题


I declared a field:

WriteableBitmap colorBitmap;

Then I created a simple thread which does something:

private void doSomething()
{
    // ... bla bla bla
    colorBitmap = new WriteableBitmap(/* parameters */);
    myImage.Source = colorBitmap; // error here:S
}

In Windows_Loaded event I declared and started a new thread:

private void window_Loaded(object sender, RoutedEventArgs e)
{
    Thread th = new Thread(new ThreadStart(doSomething));
    th.Start();
}

The problem is that I couldn't change myImage's source. I've got an error like:

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

I tried to use Dispatcher.Invoke, but it didn't help...

Application.Current.Dispatcher.Invoke((Action)delegate
{
    myImage.Source = colorBitmap;
});

I was searching for some answers, but never found the case exactly as mine. Could any1 help me to understand how to solve problems like this (I've had the same problem recently, but I couldn't call the method, because other thread owned it).


回答1:


There are two problems with your code:

  1. You can't access the WriteableBitmap from another thread that is different than the one who created it. If you want to do that, you need to freeze your bitmap by calling WriteableBitmap.Freeze() first

  2. You can't access myImage.Source in a thread that is not the dispatcher thread.

This should fix both of these two problems:

private void doSomething()
{
    // ... bla bla bla
    colorBitmap = new WriteableBitmap(/* parameters */);
    colorBitmap.Freeze();
    Application.Current.Dispatcher.Invoke((Action)delegate
    {
        myImage.Source = colorBitmap;
    });
}

EDIT Note that this approach allows you to create and update your bitmap wherever you want in your thread. Once the bitmap is frozen, it can no longer be modified in which case you should just trash it and create a new one.

On a side note, if you wish not to block your thread updating myImage.Source use BeginInvoke instead of Invoke



来源:https://stackoverflow.com/questions/11226806/thread-cannot-access-the-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!