Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are correct it is a deadlock issue.</p> <p><code>Parallel.ForEach</code> blocks till the all of the items in <code>list</code> have been processed, and you made that run on the UI thread, so now the UI thread is blocked.</p> <p>You then call <code>Form1.setText(test + "done!");</code> and <code>setText</code> (the <code>s</code> really should be capital btw) calls <code>this.Invoke</code> which will block till the invoke call has finished being processed. However because we blocked the UI thread on the <code>Parallel.ForEach</code> it will never be processed <strong>and you just deadlocked!</strong></p> <p>The way to solve this is put the <code>Parallel.ForEach</code> call on it's own thread too so you do not block the UI thread.</p> <pre><code>public void run() { Task.Factory.StartNew(() =&gt; Parallel.ForEach(list, test =&gt; { DoSomething(test); Form1.setText(test + "done!"); }), TaskCreationOptions.LongRunning); } </code></pre> <p>You also could likely improve performance by changing <code>this.Invoke</code> to <code>this.BeginInvoke</code><sup>1</sup> so the text updating is non blocking too. However, If you only make the <code>BeginInvoke</code> change to your program and leave <code>Parallel.ForEach</code> on the UI thread you may not deadlock anymore but your UI will still freeze and say <code>Not Responding</code> until <code>Parallel.ForEach</code> completes.</p> <hr> <p><sup>1. Everywhere else if you call <code>BeginXxxxx</code> you <strong><em>MUST</em></strong> call <code>EndXxxxx</code>, however <code>BeginInvoke</code> on a class derived from <code>Control</code> <a href="http://msdn.microsoft.com/en-us/library/0b1bf3y3%28v=vs.110%29.aspx#remarksToggle" rel="nofollow">is except from that rule</a> and you are not required to call <code>EndInvoke</code>.</sup></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.
 

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