Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do I not get the "Cross-thread operation not valid" error
    primarykey
    data
    text
    <p>I use a BackgroundWorker and do this:</p> <pre><code>private void loadNewAsyncToolStripMenuItem_Click(object sender, EventArgs e) { this.Text = "RunWorkerAsync()"; backgroundWorkerLoading.RunWorkerAsync(); } private void backgroundWorkerLoading_DoWork(object sender, DoWorkEventArgs e) { UnsafeThreadMethod("hello"); EvenUnsaferThreadMethod(); } </code></pre> <p>And now the two methods.</p> <pre><code>private void UnsafeThreadMethod(string text) { toolStripLabelRssFeedData.Text = text; } private void EvenUnsaferThreadMethod() { panelLoading.Visible = true; } </code></pre> <p>I don't understand why <code>UnsafeThreadMethod</code> doesn't throw the following exception but <code>EvenUnsaferThreadMethod</code> does.</p> <blockquote> <p>Cross-thread operation not valid: Control 'panelLoading' accessed from a thread other than the > thread it was created on.</p> </blockquote> <p>According to the message it's because <code>toolStripLabelRssFeedData</code> was created on the same thread but it wasn't.</p> <p>I thought that I can't call controls created by the main thread and have to use the <code>ProgressChanged</code> event. What's going on?</p> <p>And I have a second question. What is the advantage of doing it like this when I can use <code>ProgressChanged</code>? What should I do?</p> <pre><code>private void EvenUnsaferThreadMethod() { if (panelLoading.InvokeRequired) { panelLoading.Invoke(new MethodInvoker(() =&gt; { EvenUnsaferThreadMethod(); })); } else { panelLoading.Visible = true; } } </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.
 

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