Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you're using .Net 4.0 you can use a few new constructs available here, you can replace this:</p> <pre><code>foreach(string s in liste) new Thread(x =&gt; SendMail(s)).Start(); </code></pre> <p>With a <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.foreach.aspx" rel="nofollow noreferrer"><code>Parallel.ForEach</code></a>:</p> <pre><code>Parallel.ForEach(liste, SendMail); </code></pre> <p>Or, inappropriate for the case but works the <a href="http://msdn.microsoft.com/en-us/library/dd383744.aspx" rel="nofollow noreferrer"><code>.ForAll()</code></a> extension:</p> <pre><code>liste.AsParallel().ForAll(SendMail); </code></pre> <p>Someone correct me here, but what I <em>think</em> is happening is that if a Thread takes <em>any</em> time to start up it's referencing a <code>s</code> from the foreach that doesn't belong to it. The thread it passing a the value of <code>s</code> into SendMain, <em>but</em> it's <em>getting</em> that value when it's ready for it...and that may be after the <code>foreach</code> loop has already moved on, meaning that the string that <code>s</code> points to has changed.</p> <p>For example: If 2 threads start, one slow and one fast...the first slow one might get an <code>s</code> from a later <code>foreach()</code> iteration, and the faster one may get the correct <code>s</code> for the current loop. That means 2 threads got the <em>same</em> <code>s</code> reference, and the first thread copied the wrong value to pass into <code>SendMail()</code>.</p>
    singulars
    1. This table or related slice is empty.
    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.
    3. VO
      singulars
      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