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

您需要使用调度器来检查访问。您有两个选项:第一种选择:

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

相关内容

  • 没有找到相关文章

最新更新