Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The entire point of multi-threading is to allow the Thread to execute on it's own, independent of any other threads. What you want to do is use a callback to signal the completion of your thread and then have your UI respond to the completion.</p> <p>The <code>BackgroundWorker</code> class has an event already built in for this purpose.</p> <p>There are three events you want to subscribe to:</p> <pre><code>bw.DoWork += new DoWorkEventHandler(bw_DoWork); bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); </code></pre> <p><code>DoWork</code> is where your work will happen. <code>ProgressChanged</code> allows you to update the UI of progress. <code>RunWorkerCompleted</code> will pop the event with your <code>DoWork</code> function has completed.</p> <p>This object handles the threading and can be set to run asynchronously by running the <code>bw.RunWorkerAsync()</code> call.</p> <p>See the following page for detail for this:</p> <p><a href="http://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx</a></p> <p>As an example:</p> <pre><code>public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(String.Format("UI thread: {0}", Thread.CurrentThread.ManagedThreadId)); this.Invoke(new MethodInvoker(delegate() { MessageBox.Show(String.Format("Invoke thread: {0}", Thread.CurrentThread.ManagedThreadId)); })); backgroundWorker1.RunWorkerAsync(); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { MessageBox.Show(String.Format("Worker thread: {0}", Thread.CurrentThread.ManagedThreadId)); } } </code></pre> <p>This example can be built by adding one button and one background worker to a form. Wire up the events through the events designer for the button1_Click and the backgroundWorker1_DoWork function. You should have three MessagesBoxes that pop up after clicking button1. You'll notice the Id for the UI thread and the Invoke thread are the same, which means that any processing you do from the invoke will cause your UI thread to wait. The third popup is from the worker thread, which has a different ID.</p>
    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.
    2. VO
      singulars
      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