private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled) { _dialogForm.DialogResult = DialogResult.Cancel; } else { _dialogForm.DialogResult = DialogResult.OK; } _doBgWork = null; _lastDisplayUpdate = null; _dialogForm.Close(); _dialogForm.Dispose(); _dialogForm = null; if (_exceptionWhileWorking != null) { Exception localReferenceToException = _exceptionWhileWorking; _exceptionWhileWorking = null; throw new Exception("Exception encountered while executing provided work method in ProgressDialog class.", localReferenceToException); } }
/// <summary> /// Displays a modal progress dialog form and kicks off a work method using a BackgroundWorker; the modal progress dialog form will remain in the foreground until the user presses cancel AND a checkpoint in the work code is reached, or until the work completes. /// </summary> /// <param name="actionTitle">The title of the dialog form/window</param> /// <param name="initialCurrentAction">The initial status text in the window</param> /// <param name="estimatedCount">The estimated total number of work items to be performed</param> /// <param name="workMethod">The delegate method that will actually do the work</param> /// <param name="bgWorkerArgument">The object that will get passed to the delegate</param> /// <returns></returns> public DialogResult StartProgressDialog(string actionTitle, string initialCurrentAction, long estimatedCount, WorkMethod workMethod, object bgWorkerArgument) { if (_dialogForm == null) { _dialogForm = new ProgressDialogForm(); _dialogForm.btn_Cancel.Click += new EventHandler(btn_Cancel_Click); } else { throw new Exception("This class can only display one dialog at a time, per instance of the class."); } ActionTitle = actionTitle; TotalEstimatedCount = estimatedCount; CurrentCount = 0; if (!string.IsNullOrEmpty(initialCurrentAction)) { CurrentAction = initialCurrentAction; } else { CurrentAction = ""; } StartDateTime = DateTime.Now; UpdateDisplay(); //record the work method _doBgWork += workMethod; //start the worker _bgWorker.RunWorkerAsync(bgWorkerArgument); //block the underlying UI (and code flow) by showing this modal form. return(_dialogForm.ShowDialog()); }