9using System.ComponentModel;
19 private readonly System.ComponentModel.
BackgroundWorker _myWorker =
new System.ComponentModel.BackgroundWorker();
26 _myWorker.
DoWork += InternalDoWork;
34 private DoWorkEventHandler _myDoWork;
35 private RunWorkerCompletedEventHandler _myWorkerCompleted;
36 private ProgressChangedEventHandler _myWorkerProgressChanged;
41 [Description(
"Event handler to be run on a different thread when the operation begins."), Category(
"Asynchronous")]
42 public event DoWorkEventHandler
DoWork
57 [Description(
"Raised when the worker has completed (either through success, failure or cancellation)."), Category(
"Asynchronous")]
62 _myWorkerCompleted += value;
66 _myWorkerCompleted -= value;
74 [Description(
"Occurs when ReportProgress is called.).")]
79 _myWorkerProgressChanged += value;
83 _myWorkerProgressChanged -= value;
165#region Worker Async Request
167 private class WorkerAsyncRequest : ContextParams
169 public object Argument {
get;
private set; }
172 : base(applicationContext)
174 this.Argument = argument;
178 private class WorkerAsyncResult
180 public object Result {
get;
private set; }
181 public Exception
Error {
get;
private set; }
182 public bool Cancelled {
get;
private set; }
184 public WorkerAsyncResult(
object result, Exception error)
186 this.Result = result;
193#region RunWorkerAsync
224#region Private methods
233 void InternalDoWork(
object sender, DoWorkEventArgs e)
235 var request = (WorkerAsyncRequest)e.Argument;
238 request.SetThreadContext();
242 var doWorkEventArgs =
new DoWorkEventArgs(request.Argument);
243 if (_myDoWork !=
null)
245 _myDoWork.Invoke(
this, doWorkEventArgs);
247#pragma warning disable CS0618
248 e.Result =
new WorkerAsyncResult(doWorkEventArgs.Result,
null);
249#pragma warning restore CS0618
250 e.Cancel = doWorkEventArgs.Cancel;
255#pragma warning disable CS0618
256 e.Result =
new WorkerAsyncResult(
null, ex);
257#pragma warning restore CS0618
268 private void InternalRunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
272 Exception error =
null;
273 object result =
null;
277 var workerResult = (WorkerAsyncResult)e.Result;
281 error = workerResult.Error;
282 if (workerResult.Error ==
null)
284 result = workerResult.Result;
289 if (_myWorkerCompleted !=
null)
291 _myWorkerCompleted.Invoke(
this,
new RunWorkerCompletedEventArgs(result, error, e.Cancelled));
303 private void InternalProgressChanged(
object sender, ProgressChangedEventArgs e)
305 if (_myWorkerProgressChanged !=
null)
307 _myWorkerProgressChanged.Invoke(
this,
new ProgressChangedEventArgs(e.ProgressPercentage, e.UserState));
Provides consistent context information between the client and server DataPortal objects.
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...
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.
BackgroundWorker(ApplicationContext applicationContext)
Initializes a new instance of the BackgroundWorker class.
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.
@ Error
Represents a serious business rule violation that should cause an object to be considered invalid.