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

前端 未结 1 1434
无人共我
无人共我 2021-01-14 18:46

I get this error. Here is the code:

    Image image;
    BitmapImage BmpImg;
    MemoryStream ms;

    public void Convert()
    {
        ms = new MemoryStr         


        
1条回答
  •  时光说笑
    2021-01-14 19:38

    BmpImg is created on background thread. You cannot bind to Image Source DP object which is created on thread other than UI thread.

    Since you are using Dispatcher, i assume you now how to delegate stuff on UI thread.

    So all you need to do is put the creation of BmpImg on UI thread as well via Dispatcher.

    You can get UI dispatcher like this as well - App.Current.Dispatcher.

    OR

    As @Clemens suggested in comments, if you call Freeze() on BitmapImage instance, you can access it across threads.

    BmpImg.Freeze()
    

    0 讨论(0)
提交回复
热议问题