Note that there are some explanatory texts on larger screens.

plurals
  1. POC# error in Parallel.ForEach
    primarykey
    data
    text
    <p>I have a strange problem that I am unable to debug. Here's a delegate that parses information:</p> <pre><code>Action&lt;XElement, String&gt; ParseXMLInfo = (s, t) =&gt; { using (var Ctx = new Entities ()) { var Records = s.Elements ("record"); Parallel.ForEach ( ParallelEnumerable.Range (0, Records.Count ()), u =&gt; { var el = Records.ElementAt (u); try { var NTR = new tbl_UserInfo (); NTR.first_name = el.Element ("first_name").Value; NTR.last_name = el.Element ("last_name").Value; Ctx.AddTotbl_UserInfo (NTR); } catch (Exception excp) { Console.WriteLine (System.DateTime.Now + " " + excp.Message); } } ); Ctx.SaveChanges (); } }; </code></pre> <p>The delegate itself is called twice, as follows:</p> <pre><code>Parallel.Invoke ( () =&gt; { var XMLDoc_MaleInfo = XElement.Load ("MaleNames.xml"); Console.WriteLine ("Fetching records from MaleNames.xml; starting at " + System.DateTime.Now); ParseXMLInfo (XMLDoc_MaleInfo, "male"); }, () =&gt; { var XMLDoc_FemaleInfo = XElement.Load ("FemaleNames.xml"); Console.WriteLine ("Fetching records from FemaleNames.xml; starting at " + System.DateTime.Now); ParseXMLInfo (XMLDoc_MaleInfo, "female"); } ); </code></pre> <p>Everything appears to run correctly. In particular, the Parallel.ForEach portion of the delegate runs without any error. But then the code breaks at the line Ctx.SaveChanges (), with the message "Object reference not set to an instance of an object." But when I hover over Ctx (in this broken state), Ctx is not shown to be null.</p> <p>Can someone please tell me what is going on? Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    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. 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