-1

This is my exception

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

my function get results from a calculation and I want to update an already opened window..

 public override void UpdateResult(BaseMetricResults result)
        {
            var newResults = result as MetricUniformityResults;
            if (newResults == null)
            {
                return;
            }
            DispatcherHelper.UIDispatcher.Invoke(() =>
                {             
                    TopToBottomGraph.CrossSectionPoints.Clear();
                    foreach (var point in newResults.TopToBottomGraph.CrossSectionPoints)
                    {
                        TopToBottomGraph.CrossSectionPoints.Add(point);
                    }

                    newResults.JetMap.Freeze(); //exception here
                    byte[] arr = new byte[(int) (newResults.JetMap.Width*newResults.JetMap.Height*3)];
                    newResults.JetMap.CopyPixels(arr, (int) (newResults.JetMap.Width*3), 0);
                    JetMap = BitmapSource.Create((int) newResults.JetMap.Width, (int) newResults.JetMap.Height, 96, 96,
                                                 PixelFormats.Rgb24, BitmapPalettes.WebPalette, arr,
                                                 (int) (newResults.JetMap.Width*3));
                });
        }

This is my last attempt I'm not sure if I have to freeze the bitmapsource or not...
Anyway newResults.JetMap is BitmapSource, and I have a property named JetMap which is the new BitmapSource, how can I update the old image with the new one?

Gilad
  • 6,437
  • 14
  • 61
  • 119
  • Where exactly does the exception occur? Which statement? – Clemens Jan 22 '15 at 15:08
  • @Clemens newResults.JetMap.Freeze(); – Gilad Jan 22 '15 at 15:09
  • possible duplicate of [The calling thread cannot access this object because a different thread owns it](http://stackoverflow.com/questions/9732709/the-calling-thread-cannot-access-this-object-because-a-different-thread-owns-it) – Peter Duniho Jan 22 '15 at 20:05
  • If you search SO for the error message you're getting, you will get some 400 possible answers, all of which offer much the same guidance: use `Dispatcher.Invoke()`. If your scenario calls for something different, you need to provide a question with enough detail, including [a complete code example](https://stackoverflow.com/help/mcve) and a clear, precise description of why it calls for something different. – Peter Duniho Jan 22 '15 at 20:06
  • @PeterDuniho I think that you are wrong, I was just misusing function freeze(), as you can see it was solved. – Gilad Jan 25 '15 at 09:49

2 Answers2

1

Your DispatcherHelper.UIDispatcher.Invoke method will execute on the UI thread. My best guess is that the newResults.JetMap bitmap was created on a different thread which is preventing you from modifying it. At the same time, you can't create the JetMap bitmap that you want to show on a thread other than the UI thread. So without more context, the best suggestion would be to ensure that the newResults.JetMap bitmap is also created in the main UI thread.

mnistic
  • 10,866
  • 2
  • 19
  • 33
1

You need to call Jetmap.Freeze(); right after you create it and not inside the dispatcher, once its its frozen you can set it inside the dispatcher and you wont get an exception

Amit Raz
  • 5,370
  • 8
  • 36
  • 63