private void RunOnThisThread(Action <ILongWaitBroker> performWork) { LongWaitDlg longWaitDlg = null; ProgressWaitBroker progressWaitBroker; AutoResetEvent dlgCreated = new AutoResetEvent(false); AutoResetEvent workFinished = new AutoResetEvent(false); Thread monitoringThread = BackgroundEventThreads.CreateThreadForAction(() => { var dlgCreatedEvent = dlgCreated; try { using (longWaitDlg = new BackgroundThreadLongWaitDlg()) { InitLongWaitDlg(longWaitDlg); longWaitDlg.ShowInTaskbar = true; if (ParentControl != null) { var parentBounds = ParentControl.Bounds; longWaitDlg.StartPosition = FormStartPosition.Manual; longWaitDlg.Top = (parentBounds.Top + parentBounds.Bottom - longWaitDlg.Height) / 2; longWaitDlg.Left = (parentBounds.Left + parentBounds.Right - longWaitDlg.Width) / 2; } progressWaitBroker = new ProgressWaitBroker(lwb => { workFinished.WaitOne(); workFinished.Dispose(); }); dlgCreatedEvent.Set(); dlgCreatedEvent = null; longWaitDlg.PerformWork(null, DelayMillis, progressWaitBroker.PerformWork); } } finally { if (dlgCreatedEvent != null) { dlgCreated.Set(); } } }); monitoringThread.Name = "LongOperationRunnerBackgroundThread"; // Not L10N monitoringThread.Start(); dlgCreated.WaitOne(); dlgCreated.Dispose(); try { performWork(longWaitDlg); } finally { workFinished.Set(); } }
public ProgressStatus PerformWork(Control parent, int delayMillis, Action<IProgressMonitor> performWork) { var progressWaitBroker = new ProgressWaitBroker(performWork); PerformWork(parent, delayMillis, progressWaitBroker.PerformWork); return progressWaitBroker.Status; }