0

3D コンテンツを Viewport3D に非同期的に追加しようとすると、「この API は間違ったコンテキストからの引数でアクセスされました」という結果になります。TargetInvocationException で。

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

「モデル」という名前のオブジェクトは、現在のスレッドのコンテキストにないようです。どうすればこれを修正できますか? Dispatcher のコンテキストにモデルを取得する方法はありますか? それとも、これを行うこの方法は、ここに行く方法ではありませんか?

4

1 に答える 1

2

これは、シーン オブジェクトを生成/変更してビューポートに追加するたびに、ビューポートがインスタンス化された別のスレッドから発生します。簡単な回避策があります。Viewport オブジェクトを更新するコードを関数にカプセル化します。次のスニペットを挿入すれば完了です。

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); 
}
于 2010-12-09T00:43:06.660 に答える