process - 绘制到 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);
}
解决方案
您需要使用调度程序来检查访问权限。您有 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);
}
}
推荐阅读
- javascript - 从 Client.users.fetch() 踢出用户
- javascript - 使用 jQuery 和 php 进行独特的表单验证
- java - 如何删除 ViewPager2 中的无用片段
- python - Pytorch - UnboundLocalError:在为 data_loader 赋值之前引用了局部变量“img”
- python - 尝试检查我的 .JSON 文件中的用户 ID
- ffmpeg - avcodec_decode_audio4 已被弃用,我如何将大小为 avpkt->size 的音频帧从 avpkt->data 解码为帧
- c++ - 无法在 Clion 中运行 C++ cmake 项目
- rust - 强制电弧
>>>> 到弧 >>>> 不起作用 - javafx - ButtonBar:相同的按钮高度
- javascript - Bootraps 4 验证