Note that there are some explanatory texts on larger screens.

plurals
  1. POApplication hangs using PLINQ AsParallel(). No problems with LINQ
    text
    copied!<p>I'm new to LINQ and PLINQ, and I'm building a project to test them.</p> <p>Stub:</p> <pre><code>class Stub { private Boolean mytf; public Stub() { Random generator = new Random(); if (generator.NextDouble() &lt; 0.5) { mytf = false; } else mytf = true; } public Boolean tf { get { return mytf; } } } </code></pre> <p>StubCollection:</p> <pre><code>class StubCollection : IEnumerable { Stub[] stubs; public StubCollection(int n) { stubs = new Stub[n]; for (int i = 0; i &lt; n; i++) { stubs[i] = new Stub(); } } IEnumerator IEnumerable.GetEnumerator() { return new StubIterator(this); } public class StubIterator : IEnumerator { private StubCollection sc; private int index = -1; public StubIterator(StubCollection _sc) { sc = _sc; } public bool MoveNext() { index++; if (index &lt; sc.stubs.Length) { return true; } else { index = -1; return false; } } public object Current { get { if (index &lt;= -1) { throw new InvalidOperationException(); } return sc.stubs[index]; } } public void Reset() { index = -1; } } } </code></pre> <p>then I have some methods to iterate the stubcollection and count how many stubs have the boolean set to true:</p> <p>foreach:</p> <pre><code>Stopwatch sw = new Stopwatch(); Int32 n = 0; sw.Start(); foreach (Stub s in sc) if (s.tf) n++; sw.Stop(); MessageBox.Show("n:" + n.ToString() + " timer:" + sw.ElapsedMilliseconds.ToString()); </code></pre> <p>it works</p> <p>linq:</p> <pre><code>Stopwatch sw = new Stopwatch(); Int32 n = 0; sw.Start(); var trueStubs = from Stub s in sc where s.tf select s; n = trueStubs.Count(); sw.Stop(); MessageBox.Show("n:" + n.ToString() + " timer:" + sw.ElapsedMilliseconds.ToString()); </code></pre> <p>it works (little slower than foreach)</p> <p>plinq:</p> <pre><code>Stopwatch sw = new Stopwatch(); Int32 n = 0; sw.Start(); var trueStubs = from Stub s in sc.AsParallel() where s.tf select s; n = trueStubs.Count(); sw.Stop(); MessageBox.Show("n:" + n.ToString() + " timer:" + sw.ElapsedMilliseconds.ToString()); </code></pre> <p>100% CPU, no result</p> <p>why? the only difference is the AsParallel()</p>
 

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