首页 > 解决方案 > 绘制到 HelixViewPort3D 时出现 System.InvalidOperationException

问题描述

总是当我想从进程中绘制到 ViewPort3D 时,我得到一个System.InvalidOperationException.

这是什么我不明白?

进程无法访问ui进程?

我怎么解决这个问题?

            private void Pro_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Random r = new Random();
            DrawSphere(counter, Colors.Red, (double)r.Next(400) / 100);
            counter++;
        }

        private void DrawSphere(int i, Color color, double radius)
        {
            SphereVisual3D sphere = new SphereVisual3D();
            sphere.Center = new Point3D(i * 5, counter * 5, 0);
            sphere.Visible = true;
            sphere.Fill = new SolidColorBrush(color);
            sphere.Radius = radius;
            viewPort.Children.Add(sphere);
        }

标签: processhelix-3d-toolkit

解决方案


您需要使用调度程序来检查访问权限。您有 2 个选项: 第一个选项:

    private void Pro_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Random r = new Random();
            Application.Current.Dispatcher.Invoke(new Action(() => DrawSphere(counter, Colors.Red, (double)r.Next(400) / 100)));
            counter++;
        }

第二种选择:

            private void Pro_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Random r = new Random();
            DrawToViewPort(counter, Colors.Red, (double)r.Next(400) / 100);
            counter++;

        }

        private void DrawToViewPort(int i, Color color, double radius)
        {
            if (viewPort.Dispatcher.CheckAccess())
            {
                DrawSphere(i, color, radius);
            }
            else
            {
                viewPort.Dispatcher.Invoke((Action<int, Color, double>)DrawToViewPort, i, color, radius);
            }
        }

推荐阅读