Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti-threading in ASP.net MVC3 with Code-First EF
    primarykey
    data
    text
    <p>Been looking for a multi-threading solution for my application, but keep getting random <code>NullReferenceException</code> errors when I try, as opposed to when I ran each one consecutively and everything went smoothly.</p> <p>What I'm doing is calculating a journey from one place to another, and then calculating the return journey. The return journey calculation is just to use the same process, only with the departure and destinations switched.</p> <p>Does this sound feasible to do this with multi-threading in an ASP.net MVC3 app with Entity Framework? I've seen a few articles where there seem to be problems with EF, but these sound like they are only an issue when DBContexts are shared on a page, whereas I've used Using statements round all areas where I need to access the database. Anyone have any experience here?</p> <p>The basic relevant code I have so far is this:</p> <pre><code>var outboundJourneyTask = Task.Factory.StartNew(() =&gt; BuildJourney(true)); var returnJourneyTask = Task.Factory.StartNew(() =&gt; BuildJourney(false)); Task.WaitAll(outboundJourneyTask, returnJourneyTask); </code></pre> <p>What I'd hoped was that each task would start afresh, but due to the null reference exceptions, I believe that in the second thread, it is trying to use arrays built in the first, and when it can't find certain elements, it throws errors.</p> <p>Does this sound likely? Anyone know of a better way of doing this?</p> <p>EDIT: I'm trying a different method of threading (see below) but there are really strange things happening with my arrays - looping through them differently and throwing errors</p> <pre><code>Thread t1 = new Thread(ThreadMethod1); t1.Start(); Thread t2 = new Thread(ThreadMethod2); t2.Start(); void ThreadMethod1(object state) { BuildJourney(false); ManualResetEvent mre = (ManualResetEvent)state; mre.Set(); } void ThreadMethod2(object state) { BuildJourney(true); ManualResetEvent mre = (ManualResetEvent)state; mre.Set(); } </code></pre> <p>Thanks</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