You are customizing a Windows Form to asynchronously update a database in a method named WorkHandler. You need to ensure that the form displays a message box to the user that indicates the success or failure of the update.
Which code segment should you use?
A.
private void StartBackgroundProcess() {
bgwExecute.DoWork += new DoWorkEventHandler(WorkHandler);
bgwExecute.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedHandler);
bgwExecute.RunWorkerAsync();
}
void CompletedHandler(object sender, RunWorkerCompletedEventArgs e) {
bool result = (bool)e.Result;
MessageBox.Show(“Update ” + (result ? “was successful” : “failed”));
}
void WorkHandler(object sender, DoWorkEventArgs e) {
//…
e.Result = true;
}
B.
private void StartBackgroundProcess() {
bgwExecute.ProgressChanged += new ProgressChangedEventHandler(CompletedHandler);
ThreadStart tsBackground = new ThreadStart(WorkHandler);
bgwExecute.RunWorkerAsync(tsBackground); }
void ProgressHandler(object sender, ProgressChangedEventArgs e) {
bool result = (bool)e.UserState;
MessageBox.Show(“Update ” + (result ? “was successful” : “failed”));
}
void WorkHandler() {
//…
bgwExecute.ReportProgress(100, true);
}
C.
private void StartBackgroundProcess() {
bgwExecute.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedHandler);
ThreadStart tsBackground = new ThreadStart(WorkHandler);
bgwExecute.RunWorkerAsync(tsBackground);
}
void CompletedHandler(object sender, RunWorkerCompletedEventArgs e) {
bool result = (bool)e.Result;
MessageBox.Show(“Update ” + (result ? “was successful” : “failed”));
}void WorkHandler() {
//…
e.Result = true;
}
D.
private void StartBackgroundProcess() {
bgwExecute.DoWork += new DoWorkEventHandler(WorkHandler);
bgwExecute.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedHandler);
bgwExecute.RunWorkerAsync();
}
void CompletedHandler(object sender, RunWorkerCompletedEventArgs e) {
bool result = (bool)e.Result;
MessageBox.Show(“Update ” + (result ? “was successful” : “failed”));
}
void WorkHandler(object sender, DoWorkEventArgs e) {
//…
bgwExecute.ReportProgress(100, true);
}