我有下面的方法,似乎行爲奇怪。 ProgressChanged
和RunWorkerCompleted
似乎在同一時間自我更新。如果我註釋掉更新文本塊的RunWorkerCompleted
代碼,則看到數據傳輸後ProgressChanged
生效。我在這裏做錯了什麼?我顯然希望textblock顯示我正在獲取數據,然後在完成數據獲取時進行更改。WPF BackGroundWorker ProgressChanged不更新文本塊
public void GetAppointmentsBackground()
{
System.Windows.Threading.Dispatcher webServiceDispatcher = this.Dispatcher;
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += delegate(object sender, DoWorkEventArgs args)
{
GetAppointmentsForDayDelegate getAppt = new GetAppointmentsForDayDelegate(GetAppointmentsForDay);
webServiceDispatcher.BeginInvoke(getAppt);
(sender as BackgroundWorker).ReportProgress(25);
};
worker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
{
txtMessages.Text = "Contacting Server";
};
worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
txtMessages.Text = "Completed Successfully";
};
worker.RunWorkerAsync();
}
我試過了,沒有例外被拋出。另外,我沒有看到「InvokeRequired」作爲TextBlock類的一個屬性。在整晚上,我想我最好只使用直接調度員並調用,這樣我就可以獲得生命。 – 2010-05-31 11:30:57