Executes an operation on a separate thread.
Assembly: System (in System.dll)
Thetype exposes the following members.
|CancellationPending||Gets a value indicating whether the application has requested cancellation of a background operation.|
|CanRaiseEvents||Gets a value indicating whether the component can raise an event. (Inherited from Component.)|
|Container||Gets the IContainer that contains the Component. (Inherited from Component.)|
|DesignMode||Gets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)|
|Events||Gets the list of event handlers that are attached to this Component. (Inherited from Component.)|
|IsBusy||Gets a value indicating whether the is running an asynchronous operation.|
|Site||Gets or sets the ISite of the Component. (Inherited from Component.)|
|WorkerReportsProgress||Gets or sets a value indicating whether the can report progress updates.|
|WorkerSupportsCancellation||Gets or sets a value indicating whether the supports asynchronous cancellation.|
|CancelAsync||Requests cancellation of a pending background operation.|
|CreateObjRef||Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)|
|Dispose()||Releases all resources used by the Component. (Inherited from Component.)|
|Dispose(Boolean)||Releases the unmanaged resources used by the Component and optionally releases the managed resources. (Inherited from Component.)|
|Equals(Object)||Determines whether the specified object is equal to the current object. (Inherited from Object.)|
|Finalize||Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)|
|GetHashCode||Serves as the default hash function. (Inherited from Object.)|
|GetLifetimeService||Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)|
|GetService||Returns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|InitializeLifetimeService||Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)|
|MemberwiseClone()||Creates a shallow copy of the current Object. (Inherited from Object.)|
|MemberwiseClone(Boolean)||Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)|
|OnDoWork||Raises the DoWork event.|
|OnProgressChanged||Raises the ProgressChanged event.|
|OnRunWorkerCompleted||Raises the RunWorkerCompleted event.|
|ReportProgress(Int32)||Raises the ProgressChanged event.|
|ReportProgress(Int32, Object)||Raises the ProgressChanged event.|
|RunWorkerAsync()||Starts execution of a background operation.|
|RunWorkerAsync(Object)||Starts execution of a background operation.|
|ToString||Returns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from Component.)|
|Disposed||Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)|
|DoWork||Occurs when RunWorkerAsync is called.|
|ProgressChanged||Occurs when ReportProgress is called.|
|RunWorkerCompleted||Occurs when the background operation has completed, has been canceled, or has raised an exception.|
The class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the class provides a convenient solution.
To execute a time-consuming operation in the background, create a and listen for events that report the progress of your operation and signal when your operation is finished. You can create the programmatically or you can drag it onto your form from the Components tab of the Toolbox. If you create the in the Windows Forms Designer, it will appear in the Component Tray, and its properties will be displayed in the Properties window.
To set up for a background operation, add an event handler for the DoWork event. Call your time-consuming operation in this event handler. To start the operation, call RunWorkerAsync. To receive notifications of progress updates, handle the ProgressChanged event. To receive a notification when the operation is completed, handle the RunWorkerCompleted event.
For more information about , see How to: Run an Operation in the Background.
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: SharedState. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.
The following code example demonstrates the basics of the class for executing a time-consuming operation asynchronously. The following illustration shows an example of the output.
To try this code, create a Windows Forms application. Add a Label control named resultLabel and add two Button controls named startAsyncButton and cancelAsyncButton. Create Click event handlers for both buttons. From the Components tab of the Toolbox, add a component named backgroundWorker1. Create DoWork, ProgressChanged, and RunWorkerCompleted event handlers for the . In the code for the form, replace the existing code with the following code.
The following code example demonstrates the use of the class for executing a time-consuming operation asynchronously. The following illustration shows an example of the output.
The operation computes the selected Fibonacci number, reports progress updates as the calculation proceeds, and permits a pending calculation to be canceled.
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.