How can I wait for BackgroundWorker to complete?

I would like to use BackgroundWorkera database from a GUI to execute a transaction.

How can I command BackgroundWorkerto execute work, and then WAIT for execution by an employee, while maintaining a graphical interface?

Should I use DoEventsfor this purpose or is there another way?

+1
source share
4 answers

By asking, "How can I command BackgroundWorkerto complete this work, and then WAIT so that the employee can complete, while maintaining the graphical interface?" really asks: "How to use BackgroundWorker?". What does BackgroundWorker.

When you write a background task, you basically break the method into four parts:

  • Preparation for the task.
  • The task itself.
  • Report on progress in the user interface during task execution.
  • Cleaning an object when performing a task.

, . - , BackgroundWorker, DoWork, ProgressChanged RunWorkerCompleted, , , RunWorkerAsync, .

. DoWork - DoWorkEventHandler, , ReportProgress , . ProgressChanged - ProgressChangedEventHandler, ReportProgress. RunWorkerCompleted - RunWorkerCompletedEventHandler, , .

, .

, . -, , Error . , , , , , , . ( , .)

-, , DoWorkEventHandler . , MVVM, , , , , , , , - . , INotifyPropertyChanged, - , PropertyChanged .

, . . " ", . , , , , , . , .

: , , . - , - , Error , . , , , , , - .

+10

RunWorkerCompleted . , , ..

, , , .

, ?

+2

:

Application.DoEvents();

backgroundworker

, :

// The declaration of the textbox.
private TextBox m_TextBox;

// Updates the textbox text.
private void UpdateText(string text)
{
    // Set the textbox text.
    m_TextBox.Text = text;
}


public delegate void UpdateTextCallback(string text);

Invoke m_TextBox, , .

m_TextBox.Invoke(new UpdateTextCallback(this.UpdateText),
                 new object[]{"Text generated on non-UI thread."});

,

+1
source

You can call RunWorkerAsync as follows:

this.backgroundWorker1.RunWorkerAsync();

And then you will need to use the RunWorkerCompleted event .

Here is an example from MSDN:

private void backgroundWorker1_RunWorkerCompleted(
    object sender, RunWorkerCompletedEventArgs e)
{
    // First, handle the case where an exception was thrown.
    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;
}

What is it. I do not think there is a need to call DoEvents. The user interface should still respond while BackgroundWorker is running.

+1
source

Source: https://habr.com/ru/post/1746040/


All Articles