Note that there are some explanatory texts on larger screens.

plurals
  1. PONull Reference while handling a List in multiple threads
    primarykey
    data
    text
    <p>Basically, i have a collection of objects, i am chopping it into small collections, and doing some work on a thread over each small collection simultaneously.</p> <pre><code>int totalCount = SomeDictionary.Values.ToList().Count; int singleThreadCount = (int)Math.Round((decimal)(totalCount / 10)); int lastThreadCount = totalCount - (singleThreadCount * 9); Stopwatch sw = new Stopwatch(); Dictionary&lt;int,Thread&gt; allThreads = new Dictionary&lt;int,Thread&gt;(); List&lt;rCode&gt; results = new List&lt;rCode&gt;(); for (int i = 0; i &lt; 10; i++) { int count = i; if (i != 9) { Thread someThread = new Thread(() =&gt; { List&lt;rBase&gt; objects = SomeDictionary.Values .Skip(count * singleThreadCount) .Take(singleThreadCount).ToList(); List&lt;rCode&gt; result = objects.Where(r =&gt; r.ZBox != null) .SelectMany(r =&gt; r.EffectiveCBox, (r, CBox) =&gt; new rCode { RBox = r, // A Zbox may refer an object that can be // shared by many // rCode objects even on different threads ZBox = r.ZBox, CBox = CBox }).ToList(); results.AddRange(result); }); allThreads.Add(i, someThread); someThread.Start(); } else { Thread someThread = new Thread(() =&gt; { List&lt;rBase&gt; objects = SomeDictionary.Values .Skip(count * singleThreadCount) .Take(lastThreadCount).ToList(); List&lt;rCode&gt; result = objects.Where(r =&gt; r.ZBox != null) .SelectMany(r =&gt; r.EffectiveCBox, (r, CBox) =&gt; new rCode { RBox = r, // A Zbox may refer an object that // can be shared by many // rCode objects even on different threads ZBox = r.ZBox, CBox = CBox }).ToList(); results.AddRange(result); }); allThreads.Add(i, someThread); someThread.Start(); } } sw.Start(); while (allThreads.Values.Any(th =&gt; th.IsAlive)) { if (sw.ElapsedMilliseconds &gt;= 60000) { results = null; allThreads.Values.ToList().ForEach(t =&gt; t.Abort()); sw.Stop(); break; } } return results != null ? results.OrderBy(r =&gt; r.ZBox.Name).ToList():null; </code></pre> <p>so, My issue is that SOMETIMES, i get a null reference exception while performing the OrderBy operation before returning the results, and i couldn't determine where is the exception exactly, i press back, click the same button that does this operation on the same data again, and it works !! .. If someone can help me identify this issue i would be more than gratefull. NOTE :A Zbox may refer an object that can be shared by many rCode objects even on different threads, can this be the issue ? as i can't determine this upon testing, because the error happening is not deterministic.</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.
 

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