Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/backgroundworker-component-overview.md

5.0 KiB

title, ms.date, dev_langs, f1_keywords, helpviewer_keywords, ms.assetid
title ms.date dev_langs f1_keywords helpviewer_keywords ms.assetid
BackgroundWorker Component Overview 03/30/2017
csharp
vb
cpp
BackgroundWorker
BackgroundWorker component
background tasks
Asynchronous Pattern
forms [Windows Forms], multithreading
components [Windows Forms], asynchronous
forms [Windows Forms], background operations
threading [Windows Forms], background operations
background operations
64e9b3ab-7443-4a77-ab17-b8b8c0cb3f62

BackgroundWorker Component Overview

There are many commonly performed operations that can take a long time to execute. For example:

  • Image downloads

  • Web service invocations

  • File downloads and uploads (including for peer-to-peer applications)

  • Complex local computations

  • Database transactions

  • Local disk access, given its slow speed relative to memory access

Operations like these can cause your user interface to block while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the xref:System.ComponentModel.BackgroundWorker component provides a convenient solution.

The xref:System.ComponentModel.BackgroundWorker component gives you the ability to execute time-consuming operations asynchronously ("in the background"), on a thread different from your application's main UI thread. To use a xref:System.ComponentModel.BackgroundWorker, you simply tell it what time-consuming worker method to execute in the background, and then you call the xref:System.ComponentModel.BackgroundWorker.RunWorkerAsync%2A method. Your calling thread continues to run normally while the worker method runs asynchronously. When the method is finished, the xref:System.ComponentModel.BackgroundWorker alerts the calling thread by firing the xref:System.ComponentModel.BackgroundWorker.RunWorkerCompleted event, which optionally contains the results of the operation.

The xref:System.ComponentModel.BackgroundWorker component is available from the Toolbox, in the Components tab. To add a xref:System.ComponentModel.BackgroundWorker to your form, drag the xref:System.ComponentModel.BackgroundWorker component onto your form. It appears in the component tray, and its properties appear in the Properties window.

To start your asynchronous operation, use the xref:System.ComponentModel.BackgroundWorker.RunWorkerAsync%2A method. xref:System.ComponentModel.BackgroundWorker.RunWorkerAsync%2A takes an optional object parameter, which can be used to pass arguments to your worker method. The xref:System.ComponentModel.BackgroundWorker class exposes the xref:System.ComponentModel.BackgroundWorker.DoWork event, to which your worker thread is attached through a xref:System.ComponentModel.BackgroundWorker.DoWork event handler.

The xref:System.ComponentModel.BackgroundWorker.DoWork event handler takes a xref:System.ComponentModel.DoWorkEventArgs parameter, which has an xref:System.ComponentModel.DoWorkEventArgs.Argument%2A property. This property receives the parameter from xref:System.ComponentModel.BackgroundWorker.RunWorkerAsync%2A and can be passed to your worker method, which will be called in the xref:System.ComponentModel.BackgroundWorker.DoWork event handler. The following example shows how to assign a result from a worker method called ComputeFibonacci. It is part of a larger example, which you can find at How to: Implement a Form That Uses a Background Operation.

[!code-cppSystem.ComponentModel.BackgroundWorker#5] [!code-csharpSystem.ComponentModel.BackgroundWorker#5] [!code-vbSystem.ComponentModel.BackgroundWorker#5]

For more information on using event handlers, see Events.

Caution

When using multithreading of any sort, you potentially expose yourself to very serious and complex bugs. Consult the Managed Threading Best Practices before implementing any solution that uses multithreading.

For more information on using the xref:System.ComponentModel.BackgroundWorker class, see How to: Run an Operation in the Background.

See also