{"id":4471,"date":"2014-03-30T11:23:33","date_gmt":"2014-03-30T11:23:33","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-thread-seems-to-start-multiple-times-collection-of-common-programming-errors\/"},"modified":"2014-03-30T11:23:33","modified_gmt":"2014-03-30T11:23:33","slug":"c-thread-seems-to-start-multiple-times-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-thread-seems-to-start-multiple-times-collection-of-common-programming-errors\/","title":{"rendered":"C# Thread seems to start multiple times-Collection of common programming errors"},"content":{"rendered":"<p>The issue is caused by the interaction between <code>foreach<\/code> loops and lambda capture.<\/p>\n<p>The variable <code>s<\/code> is overwritten in each iteration of the <code>foreach<\/code> loop. This means that the reference to <code>s<\/code> captured by the lambda in <code>new Thread<\/code> has changed by the time the new thread is executed.<\/p>\n<p>The first thread will execute successfully, but the remainder of the threads are also pointing to the same value of <code>s<\/code> and will fail.<\/p>\n<p>The solution is to create a temporary variable:<\/p>\n<pre><code>foreach (String s in Directory.EnumerateFiles(@\"C:\\Test\", \"*.txt\", SearchOption.TopDirectoryOnly))\n{\n  var temp = s;\n  new Thread(() =&gt; File.Move(temp, temp + \"ok\")).Start();                \n}\n<\/code><\/pre>\n<p>As per @Eric Lippert&#8217;s suggestion, consider using PLINQ or the TPL to manage the threading for you:<\/p>\n<pre><code>\/\/ assume `using System.Linq`\n\nDirectory.EnumerateFiles(@\"C:\\Test\", \"*.txt\", SearchOption.TopDirectoryOnly)\n         .AsParallel()\n         .Select(f =&gt; File.move(f, f + \"ok\"))\n         .ToList();\n<\/code><\/pre>\n<p>This neatly avoids the issues with <code>foreach<\/code> and gives the runtime control over the threading behaviour.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The issue is caused by the interaction between foreach loops and lambda capture. The variable s is overwritten in each iteration of the foreach loop. This means that the reference to s captured by the lambda in new Thread has changed by the time the new thread is executed. The first thread will execute successfully, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4471","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4471"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4471\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}