Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>We are use modal form with <code>BackgroundWorker</code> for such a thing.</p> <p>Here is quick solution:</p> <pre><code> public class ProgressWorker&lt;TArgument&gt; : BackgroundWorker where TArgument : class { public Action&lt;TArgument&gt; Action { get; set; } protected override void OnDoWork(DoWorkEventArgs e) { if (Action!=null) { Action(e.Argument as TArgument); } } } public sealed partial class ProgressDlg&lt;TArgument&gt; : Form where TArgument : class { private readonly Action&lt;TArgument&gt; action; public Exception Error { get; set; } public ProgressDlg(Action&lt;TArgument&gt; action) { if (action == null) throw new ArgumentNullException("action"); this.action = action; //InitializeComponent(); //MaximumSize = Size; MaximizeBox = false; Closing += new System.ComponentModel.CancelEventHandler(ProgressDlg_Closing); } public string NotificationText { set { if (value!=null) { Invoke(new Action&lt;string&gt;(s =&gt; Text = value)); } } } void ProgressDlg_Closing(object sender, System.ComponentModel.CancelEventArgs e) { FormClosingEventArgs args = (FormClosingEventArgs)e; if (args.CloseReason == CloseReason.UserClosing) { e.Cancel = true; } } private void ProgressDlg_Load(object sender, EventArgs e) { } public void RunWorker(TArgument argument) { System.Windows.Forms.Application.DoEvents(); using (var worker = new ProgressWorker&lt;TArgument&gt; {Action = action}) { worker.RunWorkerAsync(); worker.RunWorkerCompleted += worker_RunWorkerCompleted; ShowDialog(); } } void worker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) { if (e.Error != null) { Error = e.Error; DialogResult = DialogResult.Abort; return; } DialogResult = DialogResult.OK; } } </code></pre> <p>And how we use it:</p> <pre><code>var dlg = new ProgressDlg&lt;string&gt;(obj =&gt; { //DoWork() Thread.Sleep(10000); MessageBox.Show("Background task completed "obj); }); dlg.RunWorker("SampleValue"); if (dlg.Error != null) { MessageBox.Show(dlg.Error.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } dlg.Dispose(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload