2016-09-02 2 views
0

저는 wpf 응용 프로그램을 만들고 있습니다. FlowDocument 객체를 만들어서 인쇄하고 싶습니다. 생성 단계에 몇 초가 걸리고 UI가 고정되면 코드를 새 스레드로 이동합니다. 문제는 FlowDocument에서 이미지를 설정해야하고 이미지 UIElement를 만들어야하지만 UI 컨트롤을 백그라운드 스레드에서 만들 수 없다는 것입니다. Dispather.Invoke() 시나리오도 많이 시도했지만 개체 소유자 스레드에 대한 예외가 발생했습니다.FlowDocument에 이미지 삽입

FlowDocument에 이미지를 삽입하는 다른 방법이 있습니까? 또는 백그라운드 스레드에서 이미지 UIElement를 만들 수 있습니까?

모든 의견을 환영합니다.

P.S : 일부 예제 코드 =>

BitmapImage bitmapImage = SingletonSetting.GetInstance().Logo; 
Image v = new Image() { Source = bitmapImage }; 
currnetrow.Cells.Add(new TableCell(new BlockUIContainer(v))); 




Image v = ((App)Application.Current).Dispatcher.Invoke(new Func<Image>(() => 
{ 
    BitmapImage bitmapImage = SingletonSetting.GetInstance().Logo; 
    return new Image() { Source = bitmapImage}; 
})); 
currnetrow.Cells.Add(new TableCell(new BlockUIContainer(v))); 

답변

1

당신이 BitmapImage을 수정하지 않는 경우에, 당신이 그것을 동결 ​​및 UI 스레드에서 사용할 수 있습니다. 당신이 그것에 UI 컨트롤을 만들고 있었다 때문에

// Executing on non UI Thread 
BitmapImage bitmapImage = SingletonSetting.GetInstance().Logo; 
bitmapImage.Freeze(); // Has to be done on same thread it was created on - maybe freeze it in the Singleton instead? 

Application.Current.Dispatcher.Invoke(() => { 
    // Executing on UI Thread 
    Image v = new Image() { Source = bitmapImage }; 
    currnetrow.Cells.Add(new TableCell(new BlockUIContainer(v))); 
}); 

당신과 함께 채팅을 한 후, 당신이 정말로 할 필요가 무엇을하는 STA 스레드에서 작업을 실행했다. 어떻게 그럴 수 있습니까? 이 답변 참조 : 답변

Set ApartmentState on a Task

+0

감사하지만 마지막 줄에 예외가 발생합니다. "다른 스레드가 그것을 소유하고 있기 때문에 호출하는 스레드는이 개체에 액세스 할 수 없습니다"라고 말합니다. – Evil

+0

currnetrow.Cells ... 줄? currnetrow가 만든 스레드는 무엇입니까? –

+0

예, 배경 스레드가 만듭니다! UI 스레드에서 currentrow를 사용하는 것이 문제라고 생각합니다! – Evil