viewport3d에 어린이를 추가하면 "이 API는 잘못된 맥락에서 논쟁의 여지가 있습니다."

StackOverflow https://stackoverflow.com/questions/1808417

문제

viewport3d에 3D-content를 추가하려고 할 때 비동기 적으로 "이 API는 잘못된 컨텍스트의 인수로 액세스했습니다." TargetInvocationException에서.

3D- 컨텐츠는 3D- 스캔 장치의 데이터에서 생성됩니다. 이에 필요한 통신 및 계산은 별도의 스레드에서 수행됩니다. 먼저, 나는 해당 스레드에서 viewport3d를 ACCE를 시도했습니다. 나는 이것이 Gui-Shread에서 수행해야한다는 것을 깨달았으므로 이제이 코드를 사용합니다.

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

"모델"이라는 객체는 현재 스레드의 맥락에 있지 않은 것 같습니다. 이것을 어떻게 고칠 수 있습니까? 디스패처의 맥락에 모델을 얻는 방법이 있습니까? 아니면이 방법을 수행하는 방법이 여기에가는 길이 아닌가?

도움이 되었습니까?

해결책

이것은 다른 스레드에서 뷰포트에 추가 할 장면 객체를 생성/수정할 때마다 발생합니다. 하나의 뷰포트가 인스턴스화되었습니다. 간단한 작업이 있습니다. 뷰포트 객체를 함수로 업데이트하는 코드를 캡슐화합니다. 다음 스 니펫을 삽입하면 완료됩니다.

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); 
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top