public static void CloseProgress() { if (_instance == null) { return; } if (_instance.InvokeRequired) { _instance.BeginInvoke(new MethodInvoker(() => { if (_instance.Visible) { _instance.Close(); } _instance = null; })); } else { if (_instance.Visible) { _instance.Close(); } _instance = null; } }
public static void ShowProgress(string title, Action backgroundAction, bool runInMainThread = true) { using (var form = new FormProgress()) { form.laTitle.Text = title; form.laDetails.Visible = false; form.Shown += async(o, e) => { if (_mainForm.InvokeRequired) { _mainForm.BeginInvoke(new MethodInvoker(Application.DoEvents)); } if (runInMainThread) { var taskCompleted = false; #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed Task.Run(() => { if (_mainForm.InvokeRequired) { _mainForm.BeginInvoke(new MethodInvoker(() => { Application.DoEvents(); backgroundAction(); })); } else { backgroundAction(); } taskCompleted = true; }); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed await Task.Run(() => { while (!taskCompleted) { Thread.Sleep(100); if (_mainForm.InvokeRequired) { _mainForm.BeginInvoke(new MethodInvoker(Application.DoEvents)); } } }); } else { await Task.Run(backgroundAction); } form.Close(); }; form.ShowDialog(_mainForm); } }
public static void ShowProgress(string title, Action backgroundAction, bool runInMainThread = true) { using (var form = new FormProgress()) { form.laTitle.Text = title; form.laDetails.Visible = false; form.Shown += async (o, e) => { if (_mainForm.InvokeRequired) _mainForm.BeginInvoke(new MethodInvoker(Application.DoEvents)); if (runInMainThread) { var taskCompleted = false; #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed Task.Run(() => { if (_mainForm.InvokeRequired) _mainForm.BeginInvoke(new MethodInvoker(() => { Application.DoEvents(); backgroundAction(); })); else backgroundAction(); taskCompleted = true; }); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed await Task.Run(() => { while (!taskCompleted) { Thread.Sleep(100); if (_mainForm.InvokeRequired) _mainForm.BeginInvoke(new MethodInvoker(Application.DoEvents)); } }); } else await Task.Run(backgroundAction); form.Close(); }; form.ShowDialog(_mainForm); } }