Which code segment should you use to replace line 11?

You are developing an application by using Silverlight 4 and Microsoft .NET Framework 4. You add a BackgroundWorker object named worker to the application.
You add the following code segment. (Line numbers are included for reference only.)

01 public MainPage()
02 {
03 InitializeComponent();
04 worker.WorkerSupportsCancellation = true;
05 worker.DoWork += new DoWorkEventHandler(worker_DoWork);
06 worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_Completed)
07 }
08 private void worker_DoWork(object sender, DoWorkEventArgs e)
09 {
10 for (int i = 0; i < 100; i++) {
11 InvokeLongRunningProcessStep();
12 }
13 }

You need to ensure that worker can be properly canceled. Which code segment should you use to replace line 11?

You are developing an application by using Silverlight 4 and Microsoft .NET Framework 4. You add a BackgroundWorker object named worker to the application.
You add the following code segment. (Line numbers are included for reference only.)

01 public MainPage()
02 {
03 InitializeComponent();
04 worker.WorkerSupportsCancellation = true;
05 worker.DoWork += new DoWorkEventHandler(worker_DoWork);
06 worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_Completed)
07 }
08 private void worker_DoWork(object sender, DoWorkEventArgs e)
09 {
10 for (int i = 0; i < 100; i++) {
11 InvokeLongRunningProcessStep();
12 }
13 }

You need to ensure that worker can be properly canceled. Which code segment should you use to replace line 11?

A.
var cancel = (sender as BackgroundWorker).CancellationPending;
if(cancel) {
(sender as BackgroundWorker).CancelAsync();
break;
}
else { InvokeLongRunningProcessStep();
}

B.
var cancel = (sender as BackgroundWorker).CancellationPending;
if(cancel) { e.Cancel = true; break;
}
else { InvokeLongRunningProcessStep();
}

C.
var cancel = e.Cancel;
if(cancel) {
(sender as BackgroundWorker).CancelAsync();
break;
}
else { InvokeLongRunningProcessStep();
}

D.
var cancel = e.Cancel;
if(cancel) { e.Cancel = true; break;
}
else { InvokeLongRunningProcessStep();
}



Leave a Reply 0

Your email address will not be published. Required fields are marked *