CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
BackgroundWorker.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="BackgroundWorker.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Wraps a System.ComponentModel.BackgroundWorker and transfers ApplicationContext.User, ClientContest, CurrentCulture and CurrentUICulture to background thread.</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.ComponentModel;
10
11namespace Csla.Threading
12{
17 public class BackgroundWorker : System.ComponentModel.Component
18 {
19 private readonly System.ComponentModel.BackgroundWorker _myWorker = new System.ComponentModel.BackgroundWorker();
23 public BackgroundWorker(ApplicationContext applicationContext)
24 {
25 ApplicationContext = applicationContext;
26 _myWorker.DoWork += InternalDoWork;
27 _myWorker.RunWorkerCompleted += InternalRunWorkerCompleted;
28 _myWorker.ProgressChanged += InternalProgressChanged;
29 }
30
31 private ApplicationContext ApplicationContext { get; set; }
32
33 // overridden event handler to be invoked by this class
34 private DoWorkEventHandler _myDoWork;
35 private RunWorkerCompletedEventHandler _myWorkerCompleted;
36 private ProgressChangedEventHandler _myWorkerProgressChanged;
37
41 [Description("Event handler to be run on a different thread when the operation begins."), Category("Asynchronous")]
42 public event DoWorkEventHandler DoWork
43 {
44 add
45 {
46 _myDoWork += value;
47 }
48 remove
49 {
50 _myDoWork -= value;
51 }
52 }
53
57 [Description("Raised when the worker has completed (either through success, failure or cancellation)."), Category("Asynchronous")]
58 public event RunWorkerCompletedEventHandler RunWorkerCompleted
59 {
60 add
61 {
62 _myWorkerCompleted += value;
63 }
64 remove
65 {
66 _myWorkerCompleted -= value;
67 }
68 }
69
70
74 [Description("Occurs when ReportProgress is called.).")]
75 public event ProgressChangedEventHandler ProgressChanged
76 {
77 add
78 {
79 _myWorkerProgressChanged += value;
80 }
81 remove
82 {
83 _myWorkerProgressChanged -= value;
84 }
85 }
86
93 public bool IsBusy
94 {
95 get
96 {
97 return _myWorker.IsBusy;
98 }
99 }
100
108 {
109 get
110 {
111 return _myWorker.WorkerReportsProgress;
112 }
113 set
114 {
115 _myWorker.WorkerReportsProgress = value;
116 }
117 }
118
126 {
127 get
128 {
129 return _myWorker.WorkerSupportsCancellation;
130 }
131 set
132 {
133 _myWorker.WorkerSupportsCancellation = value;
134 }
135 }
136
146 public void CancelAsync()
147 {
148 _myWorker.CancelAsync();
149 }
150
158 {
159 get
160 {
161 return _myWorker.CancellationPending;
162 }
163 }
164
165#region Worker Async Request
166
167 private class WorkerAsyncRequest : ContextParams
168 {
169 public object Argument { get; private set; }
170
171 public WorkerAsyncRequest(ApplicationContext applicationContext, object argument)
172 : base(applicationContext)
173 {
174 this.Argument = argument;
175 }
176 }
177
178 private class WorkerAsyncResult
179 {
180 public object Result { get; private set; }
181 public Exception Error { get; private set; }
182 public bool Cancelled { get; private set; }
183
184 public WorkerAsyncResult(object result, Exception error)
185 {
186 this.Result = result;
187 this.Error = error;
188 }
189 }
190
191#endregion
192
193#region RunWorkerAsync
194
204 public void RunWorkerAsync()
205 {
206 RunWorkerAsync(null);
207 }
208
216 public void RunWorkerAsync(object argument)
217 {
218 _myWorker.RunWorkerAsync(new WorkerAsyncRequest(ApplicationContext, argument));
219 }
220
221
222#endregion
223
224#region Private methods
225
233 void InternalDoWork(object sender, DoWorkEventArgs e)
234 {
235 var request = (WorkerAsyncRequest)e.Argument;
236
237 // set the background worker thread context
238 request.SetThreadContext();
239
240 try
241 {
242 var doWorkEventArgs = new DoWorkEventArgs(request.Argument);
243 if (_myDoWork != null)
244 {
245 _myDoWork.Invoke(this, doWorkEventArgs);
246 }
247#pragma warning disable CS0618 // Type or member is obsolete
248 e.Result = new WorkerAsyncResult(doWorkEventArgs.Result, null);
249#pragma warning restore CS0618 // Type or member is obsolete
250 e.Cancel = doWorkEventArgs.Cancel;
251 }
252 // must implement exception handling and return exception in WorkerAsyncResult
253 catch (Exception ex)
254 {
255#pragma warning disable CS0618 // Type or member is obsolete
256 e.Result = new WorkerAsyncResult(null, ex);
257#pragma warning restore CS0618 // Type or member is obsolete
258 }
259 }
260
268 private void InternalRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
269 {
270
271
272 Exception error = null;
273 object result = null;
274
275 if (!e.Cancelled)
276 {
277 var workerResult = (WorkerAsyncResult)e.Result;
278
279 // must check for error as accessing e.Result will throw exception
280 // if e.Error is not null.
281 error = workerResult.Error;
282 if (workerResult.Error == null)
283 {
284 result = workerResult.Result;
285 }
286 }
287
288
289 if (_myWorkerCompleted != null)
290 {
291 _myWorkerCompleted.Invoke(this, new RunWorkerCompletedEventArgs(result, error, e.Cancelled));
292 }
293 }
294
303 private void InternalProgressChanged(object sender, ProgressChangedEventArgs e)
304 {
305 if (_myWorkerProgressChanged != null)
306 {
307 _myWorkerProgressChanged.Invoke(this, new ProgressChangedEventArgs(e.ProgressPercentage, e.UserState));
308 }
309 }
310
315 public void ReportProgress(int percentProgress)
316 {
317 _myWorker.ReportProgress(percentProgress);
318 }
319
325 public void ReportProgress(int percentProgress, object userState)
326 {
327 _myWorker.ReportProgress(percentProgress, userState);
328 }
329
330#endregion
331 }
332}
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.