0

COM と Acrobat SDK を使用して PDF を印刷するアプリケーションに取り組んでいます。アプリは C#、WPF で記述されており、別のスレッドで印刷を正しく実行する方法を見つけようとしています。BackgroundWorker がスレッド プールを使用しているため、STA に設定できないことがわかりました。STA スレッドの作成方法は知っていますが、STA スレッドから進捗状況を報告する方法がわかりません。

Thread thread = new Thread(PrintMethod);
thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
thread.Start(); 
thread.Join(); //Wait for the thread to end

このように作成された STA スレッドで進行状況を WPF ViewModel に報告するにはどうすればよいですか?

4

1 に答える 1

3

実際には、UI が実行されている (既存の) STA スレッドからではなく、進行状況を報告する必要があります。

これは、BackgroundWorker関数 (ReportProgressを開始したスレッドで配信されBackgroundWorkerます -- これは UI スレッドである必要があります) を使用するか、UI スレッドDispatcher(通常は を使用) を使用して実現できますDispatcher.BeginInvoke


編集:あなたの場合、スレッドがSTAではないため
、ソリューションは機能しません。BackgroundWorkerしたがって、通常どおりに作業する必要がありますDispatcherlInvoke:

// in UI thread:
Thread thread = new Thread(PrintMethod);
thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
thread.Start();

void PrintMethod() // runs in print thread
{
    // do something
    ReportProgress(0.5);
    // do something more
    ReportProgress(1.0);
}

void ReportProgress(double p) // runs in print thread
{
    var d = this.Dispatcher;
    d.BeginInvoke((Action)(() =>
            {
                SetProgressValue(p);
            }));
}

void SetProgressValue(double p) // runs in UI thread
{
    label.Content = string.Format("{0}% ready", p * 100.0);
}

現在のオブジェクトに がない場合はDispatcher、UI オブジェクトまたはビュー モデル (使用している場合) から取得できます。

于 2012-11-05T21:51:53.677 に答える