Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please never ever use Abort(). This kind of work is best done trough BackgroundWorker; if you insist on Thread</p> <p>Try:</p> <pre><code>var form = new frmOperationProgress(); Thread progressThread = new Thread(() =&gt; Application.Run(form)); progressThread.IsBackground = true; progressThread.Start(); //Some work form.ExternalClose(); </code></pre> <p>Where ExternalClose is method of form like this:</p> <pre><code>public void ExternalClose() { if (InvokeRequired) { Invoke(new MethodInvoker(() =&gt; { ExternalClose(); })); } else { Close(); } } </code></pre> <p>Solution using BackgroundWorker:</p> <p>In backround worker you have to do UI stuff in ProgressChanged event (which is running in UI thread) and do the dirty work in DoWork event (background thread).</p> <p>FormMain.cs: (Form with single BackgroundWorker control, named "backgroundWorker1", with wired up events backgroundWorker1_DoWork, backgroundWorker1_ProgressChanged and WorkerReportsProgress set to true)</p> <pre><code>using System.ComponentModel; using System.Threading; using System.Windows.Forms; namespace ConsoleApplication1 { public partial class FormMain : Form { private FormProgress m_Form; public FormMain() { InitializeComponent(); backgroundWorker1.RunWorkerAsync(); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { backgroundWorker1.ReportProgress(0, "hello"); Thread.Sleep(2000); backgroundWorker1.ReportProgress(20, "world"); Thread.Sleep(2000); backgroundWorker1.ReportProgress(40, "this"); Thread.Sleep(2000); backgroundWorker1.ReportProgress(60, "is"); Thread.Sleep(2000); backgroundWorker1.ReportProgress(80, "simple"); backgroundWorker1.ReportProgress(100, "end"); } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { if (e.ProgressPercentage == 0 &amp;&amp; m_Form == null) { m_Form = new FormProgress(); m_Form.Show(); } if (e.ProgressPercentage == 100 &amp;&amp; m_Form != null) { m_Form.Close(); m_Form = null; return; } var message = (string)e.UserState; m_Form.UpdateProgress(e.ProgressPercentage, message); } } } </code></pre> <p>Where FormProgress is simple form with ProgressBar progressBar1 and Label label1 and one extra method:</p> <pre><code>public void UpdateProgress(int percentage, string message) { this.progressBar1.Value = percentage; this.label1.Text = message; } </code></pre>
    singulars
    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. VO
      singulars
      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