実際には、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 オブジェクトまたはビュー モデル (使用している場合) から取得できます。