private WorkerDialog CreateDialog(string caption)
        {
            if (dlg != null)
                throw new InvalidOperationException("Dialog is already running.");
            this.dlg = new WorkerDialog();

            dlg.Load += new EventHandler(dlg_Load);
            dlg.Closed += new EventHandler(dlg_Closed);
            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;
     }
 }
 public IWorkerDialog CreateWorkerDialog()
 {
     if (syncForm.InvokeRequired)
     {
         IWorkerDialog dlg = null;
         syncForm.Invoke(new Action(() =>
         {
             dlg = new WorkerDialog();
         }));
         return dlg;
     }
     else
     { 
         return new WorkerDialog();
     }
 }