Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use a <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx" rel="noreferrer">BackgroundWorker</a>. There is a <a href="http://msdn.microsoft.com/en-us/library/8xs8549b.aspx" rel="noreferrer">BackgroundWorker overview</a> on MSDN.</p> <p>Here is an example of how your code might look:</p> <pre><code>public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker backgroundWorker = (BackgroundWorker)sender; for (int i = 0; i &lt; 100; i++) { backgroundWorker.ReportProgress(i); Thread.Sleep(100); } } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { textBox1.Text = e.ProgressPercentage.ToString(); } private void button1_Click(object sender, EventArgs e) { backgroundWorker1.RunWorkerAsync(); } } </code></pre> <p>Other notes:</p> <ul> <li>Remember to set <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.workersupportscancellation.aspx" rel="noreferrer">WorkerReportsProgress</a> in the designer if you want the progress to work.</li> <li>When using a BackgroundWorker it is also often useful to use the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.progressbar.aspx" rel="noreferrer">ProgressBar</a> control.</li> <li>If you want to be able to cancel the background worker, that is possible too. See <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.runworkercompleted.aspx" rel="noreferrer">CancelAsync</a> and <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.workersupportscancellation.aspx" rel="noreferrer">WorkerSupportsCancellation</a>.</li> <li>When the background worker completes it fires the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.runworkercompleted.aspx" rel="noreferrer">RunWorkerCompleted</a> event.</li> </ul>
 

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