Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think essentially the issue is that you're accessing the <code>Pair</code> variable from within the action you are passing to Task.Factory.StartNew.</p> <pre><code>foreach (var Pair in this.Instances) { Tasks[i] = Task.Factory.StartNew(() =&gt; { this.Tasker(Pair.Value); }); i++; } </code></pre> <p>You have to realize that this means you aren't reading the variable "Pair" right away, you're telling the task factory to queue a thread pool thread. And then when that thread gets started (which could happen after an indeterminate interval) you are accessing a variable in the local scope of that thread, which has already changed because the foreach loop wasn't waiting around for the task to start.</p> <p>You can fix it like this:</p> <pre><code>foreach (var Pair in this.Instances) { var currentValue = Pair.Value; Tasks[i] = Task.Factory.StartNew(() =&gt; { this.Tasker(currentValue); }); i++; } </code></pre> <p>Now you're making a separate locally scoped variable for each iteration of the foreach which shouldn't change on you.</p> <p>As an aside, I think a separate logical problem with your code is that you are reusing the same ConcurrentQueue reference per iteration of the outer loop, but you are changing it each time. You should not have this line at all:</p> <pre><code>private ConcurrentQueue&lt;string&gt; Results { get; set; } </code></pre> <p>You should be using some better way of managing the results of each thread group. You could use a ConcurrentQueue of ConcurrentQueues in this case... but I'd personally instead use task parameters. Create the ConcurrentQueue locally in your <code>Operation</code> method and pass it to the Tasker methods as a secondary parameter.</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.
 

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