/// <summary> /// This event handler deals with the results of the background operation. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BkWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { IBackGroundLauncher shuttle = ((IBackGroundLauncher)(e.Result)); // First, handle the case where an exception was thrown. if (e.Error != null) { shuttle.Result = BGResult.Error; MessageBox.Show(e.Error.Message); } else if (e.Cancelled) { // Next, handle the case where the user canceled // the operation. // Note that due to a race condition in // the DoWork event handler, the Cancelled // flag may not have been set, even though // CancelAsync was called. shuttle.Result = BGResult.Cancelled; } else { // Finally, handle the case where the operation succeeded. shuttle.Result = BGResult.OK; } shuttle.Finished = true; }
public void CancelBackJob(IBackGroundLauncher shuttle) { _result = BGResult.Cancelled; shuttle.Result = BGResult.Cancelled; shuttle.Finished = true; FillUp(); }
/// <summary> /// This event handler is where the actual, potentially time-consuming work is done. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BkWorker_DoWork(object sender, DoWorkEventArgs e) { IBackGroundLauncher shuttle = (IBackGroundLauncher)e.Argument; try { // Get the BackgroundWorker that raised this event. BackgroundWorker worker = sender as BackgroundWorker; worker.WorkerSupportsCancellation = true; // Assign the result of the computation // to the Result property of the DoWorkEventArgs // object. This is will be available to the // RunWorkerCompleted eventhandler. worker.WorkerReportsProgress = true; shuttle.BackGroundJob(worker); shuttle.Result = BGResult.OK; } catch (Exception ex) { shuttle.Result = BGResult.Error; MessageBox.Show(iQExceptionHandler.GetAllMessages(ex)); } finally { e.Result = shuttle; } }
public void StartBackJob(IBackGroundLauncher shuttle) { try { State = BGResult.Working; _shuttle = shuttle; _bk_grow = true; RunBackGround(shuttle); shuttle.ForeGroundJob(); while (!shuttle.Finished) { if (_do_grow) { DoGrow(); } Refresh(); Application.DoEvents(); } _result = shuttle.Result; } catch (Exception ex) { _result = BGResult.Error; ShowInfoException(ex); } finally { _bk_grow = false; State = BGResult.OK; _shuttle = null; } }
/// <summary> /// Función que lanza la ejecución en segundo plano /// </summary> /// <param name="bar"></param> public virtual void RunBackGround(IBackGroundLauncher shuttle) { shuttle.Finished = false; BkWorker.RunWorkerAsync(shuttle); }