/// <summary> /// Run the internal DoWork /// - set the thread context /// - run the background workers doWork events /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param> void InternalDoWork(object sender, DoWorkEventArgs e) { var request = (WorkerAsyncRequest)e.Argument; // set the background worker thread context request.SetThreadContext(); try { var doWorkEventArgs = new DoWorkEventArgs(request.Argument); if (_myDoWork != null) { _myDoWork.Invoke(this, doWorkEventArgs); } #pragma warning disable CS0618 // Type or member is obsolete e.Result = new WorkerAsyncResult(doWorkEventArgs.Result, Csla.ApplicationContext.GlobalContext, null); #pragma warning restore CS0618 // Type or member is obsolete e.Cancel = doWorkEventArgs.Cancel; } // must implement exception handling and return exception in WorkerAsyncResult catch (Exception ex) { #pragma warning disable CS0618 // Type or member is obsolete e.Result = new WorkerAsyncResult(null, Csla.ApplicationContext.GlobalContext, ex); #pragma warning restore CS0618 // Type or member is obsolete } }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { try { lock (m_lock) m_workerThread = System.Threading.Thread.CurrentThread; m_handler.Invoke(sender, e); } catch (System.Threading.ThreadAbortException) { System.Threading.Thread.ResetAbort(); e.Cancel = true; } finally { lock (m_lock) m_workerThread = null; } }
/// <summary> /// Run the internal DoWork /// - set the thread context /// - run the background workers doWork events /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param> void InternalDoWork(object sender, DoWorkEventArgs e) { var request = (WorkerAsyncRequest)e.Argument; // set the background worker thread context SetThreadContext(request); try { var doWorkEventArgs = new DoWorkEventArgs(request.Argument); if (_myDoWork != null) { _myDoWork.Invoke(this, doWorkEventArgs); } e.Result = new WorkerAsyncResult(doWorkEventArgs.Result, Csla.ApplicationContext.GlobalContext, null); e.Cancel = doWorkEventArgs.Cancel; } // must implement exception handling and return exception in WorkerAsyncResult catch (Exception ex) { e.Result = new WorkerAsyncResult(null, Csla.ApplicationContext.GlobalContext, ex); } }
/// <summary> /// Executes the work in background to avoid DIS.Presentation.KMT hangs /// </summary> protected void WorkInBackground(DoWorkEventHandler work, RunWorkerCompletedEventHandler onCompleted = null) { //Getting called from DIS.Presentation.KMT, execute on background worker if (uiDispatcher != null) { using (BackgroundWorker worker = new BackgroundWorker()) { worker.DoWork += (s, e) => { try { Thread.CurrentThread.CurrentUICulture = KmtConstants.CurrentCulture; Thread.CurrentThread.CurrentCulture = KmtConstants.CurrentCulture; } catch (Exception ex) { ExceptionHandler.HandleException(ex); } work(s, e); }; if (onCompleted == null) onCompleted = new RunWorkerCompletedEventHandler((s, e) => { if (e.Result != null) { if (!(e.Result is Message)) throw new ApplicationException("Background worker result is invalid."); Message msg = e.Result as Message; if (msg != null) { ValidationHelper.ShowMessageBox(msg.Content, msg.Title); } } }); worker.RunWorkerCompleted += onCompleted; worker.RunWorkerAsync(); } } //Getting called from unit test, execute on main thread else { work.Invoke(null, new DoWorkEventArgs(null)); } }
/// <summary> /// Raises the <see cref="DoWork"/> event. /// </summary> /// <param name="e">The <see cref="DoWorkEventArgs"/> containing data for the event.</param> protected virtual void OnDoWork(DoWorkEventArgs e) { DoWorkEventHandler handler = DoWork; handler?.Invoke(this, e); }