9using System.ComponentModel;
10using System.Globalization;
11using System.Threading;
21 private readonly System.ComponentModel.
BackgroundWorker _myWorker =
new System.ComponentModel.BackgroundWorker();
27 _myWorker.
DoWork += InternalDoWork;
33 private DoWorkEventHandler _myDoWork;
34 private RunWorkerCompletedEventHandler _myWorkerCompleted;
35 private ProgressChangedEventHandler _myWorkerProgressChanged;
40 [Description(
"Event handler to be run on a different thread when the operation begins."), Category(
"Asynchronous")]
41 public event DoWorkEventHandler
DoWork
56 [Description(
"Raised when the worker has completed (either through success, failure or cancellation)."), Category(
"Asynchronous")]
61 _myWorkerCompleted += value;
65 _myWorkerCompleted -= value;
73 [Description(
"Occurs when ReportProgress is called.).")]
78 _myWorkerProgressChanged += value;
82 _myWorkerProgressChanged -= value;
164#region Worker Async Request
166 private class WorkerAsyncRequest : ContextParams
168 public object Argument {
get;
private set; }
170 public WorkerAsyncRequest(
object argument)
172 this.Argument = argument;
176 private class WorkerAsyncResult
178 public object Result {
get;
private set; }
180 public Exception
Error {
get;
private set; }
181 public bool Cancelled {
get;
private set; }
185 this.Result = result;
204#region RunWorkerAsync
235#region Private methods
244 void InternalDoWork(
object sender, DoWorkEventArgs e)
246 var request = (WorkerAsyncRequest)e.Argument;
249 request.SetThreadContext();
253 var doWorkEventArgs =
new DoWorkEventArgs(request.Argument);
254 if (_myDoWork !=
null)
256 _myDoWork.Invoke(
this, doWorkEventArgs);
258#pragma warning disable CS0618
260#pragma warning restore CS0618
261 e.Cancel = doWorkEventArgs.Cancel;
266#pragma warning disable CS0618
268#pragma warning restore CS0618
279 private void InternalRunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
283 Exception error =
null;
284 object result =
null;
288 var workerResult = (WorkerAsyncResult)e.Result;
295 error = workerResult.Error;
296 if (workerResult.Error ==
null)
298 result = workerResult.Result;
303 if (_myWorkerCompleted !=
null)
305 _myWorkerCompleted.Invoke(
this,
new RunWorkerCompletedEventArgs(result, error, e.Cancelled));
317 private void InternalProgressChanged(
object sender, ProgressChangedEventArgs e)
319 if (_myWorkerProgressChanged !=
null)
321 _myWorkerProgressChanged.Invoke(
this,
new ProgressChangedEventArgs(e.ProgressPercentage, e.UserState));
Dictionary type that is serializable with the SerializationFormatterFactory.GetFormatter().
A BackgroundWorker that wraps a System.ComponentModel.BackgroundWorkertransfers ApplicationContext....
void ReportProgress(int percentProgress)
Calls report progress on the underlying background worker.
bool WorkerReportsProgress
Gets or sets a value indicating whether the T:System.ComponentModel.BackgroundWorker can report progr...
Csla.Core.ContextDictionary GlobalContext
Gets a reference to the global context returned from the background thread and/or server.
DoWorkEventHandler DoWork
Occurs when M:System.ComponentModel.BackgroundWorker.RunWorkerAsync is called.
RunWorkerCompletedEventHandler RunWorkerCompleted
Occurs when the background operation has completed, has been canceled, or has raised an exception.
ProgressChangedEventHandler ProgressChanged
Occurs when M:System.ComponentModel.BackgroundWorker.ReportProgress is called.
bool IsBusy
Gets a value indicating whether the T:System.ComponentModel.BackgroundWorker is running an asynchrono...
void CancelAsync()
Request cancelation of async operation.
bool CancellationPending
Gets a value indicating whether the application has requested cancellation of a background operation.
bool WorkerSupportsCancellation
Gets or sets a value indicating whether the T:System.ComponentModel.BackgroundWorker supports asynchr...
void ReportProgress(int percentProgress, object userState)
Calls report progress on the background worker.
void RunWorkerAsync()
Starts execution of a background operation.
void RunWorkerAsync(object argument)
Starts execution of a background operation.
BackgroundWorker()
Initializes a new instance of the BackgroundWorker class.
@ Error
Represents a serious business rule violation that should cause an object to be considered invalid.