[英]Proper way to cancel BackgroundWorker
這是我第一次使用wpf。 現有系統必須處理excel文件,現在的要求是,excel文件必須有五個逗號。 這是要做處理的代碼
void InsertIDsNamesAndAddWorker_DoWork(object sender, DoWorkEventArgs e)
{
// read the excel file here
int columns = xlWorkSheet.UsedRange.Columns.Count;
if (columns == 5)
{
//do your normal processing
}
else
{
//if requirements are not met then display error message
System.Windows.MessageBox.Show("There must be five columns in this
file", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
現在,此代碼確實進入了其他部分,但是繼續到其他部分,然后顯示一條錯誤消息,指出“處理完成”。
這是執行確認消息的方法的當前代碼
void InsertIDsNamesAndAddWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
ProgressBarValue = 100;
StatusLable = "Done Processing.";
if (System.Windows.MessageBox.Show("Done Processing.", "Status", MessageBoxButton.OK, MessageBoxImage.Information) == MessageBoxResult.OK)
{
StatusLable = string.Empty;
ProgressBarValue = 0;
}
}
現在,我是wpf技術的新手,我意識到Statuslable的硬編碼值是引起問題的一個原因,因此,如果滿足要求並已完成處理,則我將ProgressBarValue設置為100。 如果列不等於5,我還將ProgressBarValue設置為零。這是新代碼
void InsertIDsNamesAndAddWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
int count = ProgressBarValue;
if (count != 100)
{
StatusLable = string.Empty;
ProgressBarValue = 0;
}
else
{
//ProgressBarValue = 100;
StatusLable = "Done Processing.";
if (System.Windows.MessageBox.Show("Done Processing.", "Status", MessageBoxButton.OK, MessageBoxImage.Information) == MessageBoxResult.OK)
{
StatusLable = string.Empty;
ProgressBarValue = 0;
}
}
}
我的主要問題是,這是否正確? 如果不滿足要求,我還有其他方法可以取消工作嗎?
BackgroundWorker具有一個名為WorkerSupportsCancellation
的屬性。 如果將其設置為true,則可以選擇另一個取消執行的選項。
每當發生錯誤時,都可以調用backgroundWorker.CancelAsync()
,該方法會將布爾值設置為true(這是BackgroundWorker對象中的CancellationPending
屬性)。
然后,您可以在執行期間檢查CancellationPending
是否為true。 如果是這樣,工人應該停下來。
如果工作程序停止,它將啟動RunWorkerCompleted
事件,該事件將最終在方法的處理程序中(如果添加了任何事件)。
可以在所有指令處或在for循環開始時檢查這種取消方式(如: for (int i = 0; (i < x) && worker.CancellationPending; i++) ;
)
希望這可以幫助!
使用在DoWork中傳遞的DoWorkEventArgs實例的結果屬性:
void InsertIDsNamesAndAddWorker_DoWork(object sender, DoWorkEventArgs e)
{
if (columns == 5)
{
//do your normal processing
e.Result = true; // we're OK
}
else
{
//if requirements are not met then display error message
System.Windows.MessageBox.Show("There must be five columns in this
file", MessageBoxButton.OK, MessageBoxImage.Error);
e.Result = false; // something wrong
}
}
然后在RunWorkerCompleted中檢查Result值並進行相應處理。
void InsertIDsNamesAndAddWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// check if we're not cancelled or error-ed before checking the Result
result = (!e.Cancelled && e.Error == null)? (bool) e.Result: false; // what is the outcome
ProgressBarValue = 100;
if (result) {
StatusLable = "Done Processing.";
if (System.Windows.MessageBox.Show("Done Processing.", "Status", MessageBoxButton.OK, MessageBoxImage.Information) == MessageBoxResult.OK)
{
StatusLable = string.Empty;
ProgressBarValue = 0;
}
}
else
{
StatusLable = "Error in Excel sheet";
}
}
請注意, Result
是object類型。 您可以將類型的任何實例放入其中,甚至可以放入您自己的類,如果您想返回關於出問題的更細粒度的詳細信息,可能需要使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.