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 SetTitle(string text, bool withDetails = false) { if (_instance == null) { _instance = new FormProgress(); } if (_instance.InvokeRequired) { _instance.BeginInvoke(new MethodInvoker(() => { _instance.laTitle.Text = text; _instance.laDetails.Visible = withDetails; SetDetails(String.Empty); Application.DoEvents(); })); } else { _instance.laTitle.Text = text; _instance.laDetails.Visible = withDetails; SetDetails(String.Empty); } }
public static void CloseProgress() { if (_instance == null) return; if (_instance.InvokeRequired) _instance.BeginInvoke(new MethodInvoker(() => { _instance.Close(); _instance = null; })); else { _instance.Close(); _instance = null; } }
public static void ShowProgress() { if (_instance == null) _instance = new FormProgress(); _instance.Closed += (o, e) => { _instance.Dispose(); _instance = null; }; _instance.Show(_mainForm); }
public static void SetDetails(string text) { if (_instance == null) _instance = new FormProgress(); _instance.BeginInvoke(new MethodInvoker(() => { _instance.laDetails.Text = text; Application.DoEvents(); })); }
public static void SetTitle(string text, bool withDetails = false) { if (_instance == null) _instance = new FormProgress(); _instance.BeginInvoke(new MethodInvoker(() => { _instance.laTitle.Text = text; _instance.laDetails.Visible = withDetails; SetDetails(String.Empty); Application.DoEvents(); })); }