Restart a BackroundWorker Pattern

Tags: backgroundWorker

If you use a BackgroundWorker in your .Net Application you probably need to cancel the running process and re-start it with new parameters.
There is a Pattern that you should use, to avoid the “BackgroundWorker is busy” Error.
 

/// <summary>
/// This flag is used to restart the BackroundWorker process
/// </summary>
private bool _restartSearchBackroundWorker;
 
 
/// <summary>
/// This starts the Backroundworker process
/// </summary>
void StartSearch() {
 
    // check if the worker is already running
    if (searchBackgroundWorker.IsBusy) {
 
        // we want the BackgroundWorker to restart when it's finnished
        _restartSearchBackroundWorker = true;
 
        // Cancel current background operations
        searchBackroundWorker.CancelAsync();
    } else {
 
        // Start the operation - here we start retrieving Data of a SQL database with Searchtext
        searchBackroundWorker.RunWorkerAsync(textBoxSearchInput.Text);
    }
}
 
 
/// <summary>
/// This cancels the Backgroundworker
/// Can be used either by the Application, or in the Form closed event
/// </summary>
private void CancelSearch() {
    if (searchBackgroundWorker.IsBusy) {
 
        // No - we won't restart the BackgroundWorker
        _restartSearchBackroundWorker = false;
 
        // Cancel current background operations
        searchBackgroundWorker.CancelAsync();
    }
}
 
/// <summary>
/// The BackgroundWorker DoWork Method
/// </summary>
private void searchBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
 
    BackgroundWorker w = (BackgroundWorker)sender;
 
    // this is a class that generates a SQL request for our application
    SearchFilter filter = new SearchFilter(e.Argument.ToString());
 
    // usually here you would do a long running process, that would block the user interface
    // in a for-each loop you have to check the 
    // w.CancellationPending parameter and exit the loop when it's set
    DataService dataService = new DataService(Globals.Settings.SqlConnectionString);
    e.Result = dataService.ExecuteQuery(filter);
}
 
/// <summary>
/// This is the RunWorkerCompleted event.
/// Itis called when the BackgroundWorker has finnished the operation
/// </summary>
private void searchBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
 
    // When the operation has been canceled
    if (e.Cancelled) {
 
        // check if the BackgroundWorker has to be re-started
        if (_restartSearchBackroundWorker) {
 
            // If you want to re-start, clear the flag
            _restartSearchBackroundWorker = false;
 
            // re-start the BackgroundWorker
            downLoader.RunWorkerAsync(textBoxSearchInput.Text);
        }
    } else {
        // When the operation has not been canceled, use the result 
        IEnumerable<IClientRow> result = (IEnumerable<IClientRow>)e.Result;
        // TODO: Do something with the result
    }
}

Happy Coding,

Helmut

Add a Comment