2016-09-02 16 views
0

私はwpfアプリケーションで作業しています。 FlowDocumentオブジェクトを作成して印刷したいと思います。作成に数秒かかり、UIがフリーズするので、コードを新しいスレッドに移動します。問題は、FlowDocumentにImageを設定し、Image 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