添加一个孩子Viewport3D异步给人"这个API是带参数访问从错误的上下文中,QUOT。 [英] Adding a child to Viewport3D asynchronously gives "This API was accessed with arguments from the wrong context."
问题描述
当我尝试3D内容添加到Viewport3D,异步,这将导致这个API是从错误的环境参数进行访问。在TargetInvocationException。
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.
的3D-内容从3D-扫描装置的数据生成的。通信与放大器,需要的计算都在一个单独的线程中完成的。首先,我试图从该线程存取权限的viewport3D。我意识到,这应该由GUI线程来完成,所以现在我用这个code:
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?
推荐答案
这会当过您生成/修改场景对象添加到视口,从不同的线程,然后一个视口被实例化的发生。有一个简单的变通。封装code,更新视口对象到一个函数。将下面的代码片段,和你做。
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.
private delegate void MyFunctionDelegate();
void MyFunction()
{
if(!Application.Current.Dispatcher.CheckAccess())
{
Application.Current.Dispatcher.Invoke(new MyFunctionDelegate(MyFunction));
return; // Important to leave the culprit thread
}
.
.
.
this.Viewport3D.Children.Remove(model);
MyModifyModel(model);
this.Viewport3D.Children.Add(model);
}
这篇关于添加一个孩子Viewport3D异步给人"这个API是带参数访问从错误的上下文中,QUOT。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!