Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe BackgroundWorker is designed to automatically utilize a new thread. Therefore creating a new thread just to call RunWorkerAsync is redundant. You are creating a thread just to create yet another thread. What's probably happening is this:</p> <ol> <li>You create a new thread from thread 1 (the GUI thread); call this thread 2.</li> <li>From thread 2, you launch RunWorkerAsync which itself creates yet another thread; call this thread 3.</li> <li>The code for RunWorkerCompleted runs on thread 2, which is the thread that called RunWorkerAsync.</li> <li>Since thread 2 is not the same as the GUI thread (thread 1), you get an illegal cross-thread call exception.</li> </ol> <p>(The below suggestion uses VB instead of C# since that's what I'm more familiar with; I'm guessing you can figure out how to write the appropriate C# code to do the same thing.)</p> <p>Get rid of the extraneous new thread; just declare _worker WithEvents, add handlers to _worker.DoWork and _worker.RunWorkerCompleted, and then call _worker.RunWorkerAsync instead of defining a custom PerformWorkerTask function.</p> <p><strong>EDIT</strong>: To update GUI controls in a thread-safe manner, use code like the following (more or less copied from <a href="http://msdn.microsoft.com/en-us/library/ms171728.aspx" rel="nofollow noreferrer">this article from MSDN</a>):</p> <pre><code>delegate void SetTextCallback(System.Windows.Forms.Control c, string t); private void SafeSetText(System.Windows.Forms.Control c, string t) { if (c.InvokeRequired) { SetTextCallback d = new SetTextCallback(SafeSetText); d.Invoke(d, new object[] { c, t }); } else { c.Text = t; } } </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. 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