总是当我想从进程中导入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);
}
答案 0 :(得分:0)
您已使用调度程序检查访问。您有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);
}
}