up as a RunWorkerCompletedEventHandler then you've set yourself up with a real trooper of a BackgroundWorker. All hell could break loose during its processing but it won't make a peep!
Spinning off threads via:
Thread th = new Thread(new ThreadStart(myThreadStartMethod));
th.Start();
Is not nearly so tolerant.
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// IF YOU DON'T CHECK e.Error != null THEN BAD THINGS CAN
// HAPPEN IN YOUR WORKER BUT NO ONE WILL EVER KNOW!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if(e.Error != null) {
MessageBox.Show(e.Error.Message);
}
else if(e.Cancelled) {
// Next, handle the case where the user canceled
// the operation.
// Note that due to a race condition in
// the DoWork event handler, the Cancelled
// flag may not have been set, even though
// CancelAsync was called.
resultLabel.Text = "Canceled";
}
else {
// Finally, handle the case where the operation
// succeeded.
resultLabel.Text = e.Result.ToString();
}
// Enable the UpDown control.
this.numericUpDown1.Enabled = true;
// Enable the Start button.
startAsyncButton.Enabled = true;
// Disable the Cancel button.
cancelAsyncButton.Enabled = false;
}
No comments:
Post a Comment