Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I started out writing a comment, but it quickly became too long.</p> <p>First off, I agree that ChrisK's answer is, indeed, a good solution to the problem. As an academic exercise, however, I think it's worth explaining <em>why</em> the issue happens in the first place.</p> <p><code>Parallel.ForEach</code> blocks the thread it's called on (which is the UI thread in your case) until all parallel operations have completed. At the same time you've got other threads trying to synchronously invoke actions on the UI thread (which is already blocked). Those invokes have to wait for the UI thread to unblock, but it won't unblock until all non-UI threads have finished their work (which they can't, because they're waiting). Checkmate. You have a deadlock, and your call to <code>Parallel.ForEach</code> will never complete.</p> <p>Realistically you could have solved your issue by simply substituting <code>this.Invoke</code> with <code>this.BeginInvoke</code>, which posts work to the UI thread asynchronously and thus allows non-UI threads to keep going and eventually complete; however I do maintain that offloading the actual call to <code>Parallel.ForEach</code> to the thread pool via a <code>Task</code> (as suggested by ChrisK) is a better solution all around.</p> <p>P.S. On an unrelated note, your call to <code>Thread.CurrentThread.ManagedThreadId</code> is always invoked on the UI thread, and therefore will always return <em>the ID of your UI thread</em>, which may or may not be the thread doing the work on the image. If you want to know which actual thread is doing the work you would have to store the return value from <code>Thread.CurrentThread.ManagedThreadId</code> outside of your delegate definition, and then close over it.</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. 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