public ProgressCounter(Thread workerThread, int maximumValue, int initialValue = 0, int incrementBy = 1) { _workerThread = workerThread; _childProgressChangedHandler = new ProgressChangedHandler(_childCounter_ProgressChanged); _childStatusChangedHandler = new StatusChangedHandler(_childCounter_StatusChanged); _value = initialValue; _incrementBy = incrementBy; _maximum = maximumValue; ProgressCounter parentCounter = Top; if (parentCounter == null) { // Set top level counter Top = this; } else { // Add to stack while (parentCounter._childCounter != null) { parentCounter = parentCounter._childCounter; } if (parentCounter != null) { parentCounter.AddChild(this); } } }
public void RemoveChild() { if (_childCounter != null) { _childCounter._parentCounter = null; _childCounter.ProgressChanged -= _childProgressChangedHandler; _childCounter.StatusChanged -= _childStatusChangedHandler; _childCounter = null; ProcessStatusChanged(); } }
public void AddChild(ProgressCounter child) { if (_childCounter == child) { return; } RemoveChild(); _childCounter = child; _childCounter.ProgressChanged += _childProgressChangedHandler; _childCounter.StatusChanged += _childStatusChangedHandler; _childCounter._parentCounter = this; }
public void ShowDialog(System.Windows.Forms.Control owner, string text, string caption, Utils.ProgressDialog.Animations animation = ProgressDialog.Animations.None) { Exception workerException = null; var workerThread = new System.Threading.Thread(() => { try { WorkerAction.Invoke(); } catch (Exception e) { workerException = e; } }); workerThread.Name = "ProgressCounterDialog worker"; var dialogUpdateEvent = new AutoResetEvent(false); var dialogCloseEvent = new ManualResetEvent(false); using (var workerProgress = new ProgressCounter(workerThread, 1)) { workerProgress.StatusChanged += (List <string> status) => { dialogUpdateEvent.Set(); }; workerProgress.ProgressChanged += (double percent) => { dialogUpdateEvent.Set(); }; workerThread.Start(); var dialog = new Utils.ProgressDialog(); dialog.Title = caption; dialog.Line1 = text; dialog.Line2 = workerProgress.Status; dialog.Animation = animation; dialog.CancelMessage = "Please wait while the operation is cancelled"; dialog.Maximum = 100; dialog.Value = (uint)(workerProgress.GetProgress() * dialog.Maximum); dialog.Modal = true; dialog.NoTime = true; IntPtr handle = IntPtr.Zero; if (owner != null) { if (owner.InvokeRequired) { owner.Invoke(new Action(delegate { handle = owner.Handle; })); } else { handle = owner.Handle; } } dialog.ShowDialog(handle); while (workerThread.IsAlive) { if (dialog.HasUserCancelled) { workerProgress.Abort(); Cancelled = true; break; } int waitResult = WaitHandle.WaitAny(new WaitHandle[] { dialogUpdateEvent, dialogCloseEvent }, 50); if (waitResult == WaitHandle.WaitTimeout) { continue; } if (waitResult == 1) // dialogCloseEvent { break; } var statusList = workerProgress.GetStatusList(); if (statusList.Count >= 1) { dialog.Line2 = statusList[0]; if (statusList.Count >= 2) { dialog.Line3 = statusList[1]; } else { dialog.Line3 = ""; } } else { dialog.Line2 = ""; } dialog.Value = (uint)(workerProgress.GetProgress() * dialog.Maximum); } dialog.CloseDialog(); dialog = null; if (workerException != null) { throw new Exception(workerException.Message, workerException); } if (owner != null) { owner.Invoke(new Action(delegate { var form = owner as System.Windows.Forms.Form; if (form != null) { form.Activate(); } else { owner.Focus(); } })); } } }