public IWorkerDialog CreateWorkerDialog()
 {
     if (syncForm.InvokeRequired)
     {
         IWorkerDialog dlg = null;
         syncForm.Invoke(new Action(() =>
         {
             dlg = new WorkerDialog();
         }));
         return(dlg);
     }
     else
     {
         return(new WorkerDialog());
     }
 }
 private IWorkerDialog CreateDialog(string caption)
 {
     if (dlg != null)
         throw new InvalidOperationException("Dialog is already running.");
     this.dlg = sp.RequireService<IDialogFactory>().CreateWorkerDialog();
     this.cancellationSvc = new CancellationTokenSource();
     this.sp.RequireService<IServiceContainer>().AddService<CancellationTokenSource>(cancellationSvc);
     dlg.Load += new EventHandler(dlg_Load);
     dlg.Closed += new EventHandler(dlg_Closed);
     dlg.CancellationButton.Click += dlg_Cancelled;
     dlg.Worker.WorkerSupportsCancellation = true;
     dlg.Worker.WorkerReportsProgress = true;
     dlg.Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
     dlg.Worker.ProgressChanged += new ProgressChangedEventHandler(Worker_ProgressChanged);
     dlg.Worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
     dlg.DialogResult = DialogResult.Cancel;
     dlg.Caption.Text = caption;
     return dlg;
 }
 private IWorkerDialog CreateDialog(string caption)
 {
     if (dlg != null)
     {
         throw new InvalidOperationException("Dialog is already running.");
     }
     this.dlg             = sp.RequireService <IDialogFactory>().CreateWorkerDialog();
     this.cancellationSvc = new CancellationTokenSource();
     this.sp.RequireService <IServiceContainer>().AddService <CancellationTokenSource>(cancellationSvc);
     dlg.Load   += new EventHandler(dlg_Load);
     dlg.Closed += new EventHandler(dlg_Closed);
     dlg.CancellationButton.Click         += dlg_Cancelled;
     dlg.Worker.WorkerSupportsCancellation = true;
     dlg.Worker.WorkerReportsProgress      = true;
     dlg.Worker.RunWorkerCompleted        += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
     dlg.Worker.ProgressChanged           += new ProgressChangedEventHandler(Worker_ProgressChanged);
     dlg.Worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
     dlg.DialogResult   = DialogResult.Cancel;
     dlg.Caption.Text   = caption;
     return(dlg);
 }
 /// <summary>
 /// The UI thread requests a background operation by calling this method.
 /// </summary>
 /// <param name="caption"></param>
 /// <param name="backgroundTask"></param>
 /// <returns></returns>
 public bool StartBackgroundWork(string caption, Action backgroundTask)
 {
     lastException = null;
     try
     {
         this.task = backgroundTask;
         using (dlg = CreateDialog(caption))
         {
             if (uiSvc.ShowModalDialog(dlg) == DialogResult.OK)
             {
                 return(true);
             }
             if (lastException != null)
             {
                 uiSvc.ShowError(lastException, "{0}", caption);
             }
             return(false);
         }
     }
     finally
     {
         dlg = null;
     }
 }
 /// <summary>
 /// The UI thread requests a background operation by calling this method.
 /// </summary>
 /// <param name="caption"></param>
 /// <param name="backgroundTask"></param>
 /// <returns></returns>
 public bool StartBackgroundWork(string caption, Action backgroundTask)
 {
     lastException = null;
     try
     {
         this.task = backgroundTask;
         using (dlg = CreateDialog(caption))
         {
             if (uiSvc.ShowModalDialog(dlg) == DialogResult.OK)
                 return true;
             if (lastException != null)
                 uiSvc.ShowError(lastException, "{0}", caption);
             return false;
         }
     }
     finally
     {
         dlg = null;
     }
 }