向 Viewport3D 异步添加子项会给出“此 API 是使用来自错误上下文的参数访问的。”
当我尝试将 3D 内容异步添加到 Viewport3D 时,会导致“使用来自错误上下文的参数访问此 API”。在 TargetIncationException 中。
3D 内容是根据 3D 扫描设备的数据生成的。所需的通信和计算是在单独的线程中完成的。首先,我尝试从该线程访问 viewport3D。我意识到这应该由 GUI 线程完成,所以现在我使用这段代码:
ModelVisual3D model = new ModelVisual3D();
model.Content = scanline;
DispatcherOperation dispOp = this.viewport.Dispatcher.BeginInvoke(
new AddModelDelegate(StartAddModel), model);
}
private void StartAddModel(ModelVisual3D model)
{
this.viewport.Children.Add(model);
//model is not in the context of this current thread.
//Throws exception: "This API was accessed with arguments from the wrong context."
}
private delegate void AddModelDelegate(ModelVisual3D model);
看起来名为“model”的对象不在当前线程的上下文中。我该如何解决这个问题?有没有办法让模型进入调度程序的上下文? 或者这种做法不适合这里?
When I try to add 3D-content to a Viewport3D, asynchronously, this results in "This API was accessed with arguments from the wrong context." in a TargetInvocationException.
The 3D-content is generated from the data of a 3D-scanning device. The communication&calculations needed for that are done in a separate thread. First, I tried to acces the viewport3D from that thread. I realized this should be done by the GUI-thread, so now I use this code:
ModelVisual3D model = new ModelVisual3D();
model.Content = scanline;
DispatcherOperation dispOp = this.viewport.Dispatcher.BeginInvoke(
new AddModelDelegate(StartAddModel), model);
}
private void StartAddModel(ModelVisual3D model)
{
this.viewport.Children.Add(model);
//model is not in the context of this current thread.
//Throws exception: "This API was accessed with arguments from the wrong context."
}
private delegate void AddModelDelegate(ModelVisual3D model);
It seems that the object named "model" is not in the context of the current thread. How can I fix this? Is there a way to get the model to the context of the Dispatcher?
Or is this way of doing this just not the way to go here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您从实例化视口的不同线程生成/修改场景对象以添加到视口时,就会发生这种情况。有一个简单的解决方法。将更新 Viewport 对象的代码封装到一个函数中。插入以下代码片段即可完成。
This will occur when ever you generate/modify scene objects to add to Viewport, from a different thread then the one Viewport was instantiated on. There is a simple work around. Encapsulate the code that updates Viewport objects into a function. Insert the following snippet and you are done.