-1

这是我的例外

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

我的函数从计算中获取结果,我想更新一个已经打开的窗口..

 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));
                });
        }

这是我的最后一次尝试,我不确定是否必须冻结位图源...
无论如何 newResults.JetMap 是 BitmapSource,并且我有一个名为 JetMap 的属性,它是新的 BitmapSource,如何更新旧图像新的那一个?

4

2 回答 2

1

您的 DispatcherHelper.UIDispatcher.Invoke 方法将在 UI 线程上执行。我最好的猜测是 newResults.JetMap 位图是在不同的线程上创建的,这会阻止您对其进行修改。同时,您不能创建要在 UI 线程以外的线程上显示的 JetMap 位图。因此,如果没有更多上下文,最好的建议是确保 newResults.JetMap 位图也在主 UI 线程中创建。

于 2015-01-22T17:37:32.377 回答
1

您需要Jetmap.Freeze();在创建后立即调用,而不是在调度程序内部调用,一旦它被冻结,您可以在调度程序内部设置它并且您不会收到异常

于 2015-01-22T20:32:48.987 回答