Reputation: 1605
Been looking for a multi-threading solution for my application, but keep getting random NullReferenceException
errors when I try, as opposed to when I ran each one consecutively and everything went smoothly.
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.
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?
The basic relevant code I have so far is this:
var outboundJourneyTask = Task.Factory.StartNew(() => BuildJourney(true));
var returnJourneyTask = Task.Factory.StartNew(() => BuildJourney(false));
Task.WaitAll(outboundJourneyTask, returnJourneyTask);
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.
Does this sound likely? Anyone know of a better way of doing this?
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
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();
}
Thanks
Upvotes: 4
Views: 1002
Reputation: 949
"Does this sound feasible..?" Yes, most certainly.
Easy to implement..... maybe not.
Thread t1 = new Thread(ThreadMethod1)
is an outdated but not necessarily deprecated way to do multithreading in .net 4+ framework. From what you have posted there is nothing indicating that you can't use tasks. Task factory is more modern way of managing Tasks in TPL but I have found it to have some quirks, personally. If I were you I would try doing this:
Task T = new Task(() =>
{
BuildJourney(true);
});
Task T2 = T.ContinueWith((antecedent) =>
{
BuildJourney(false);
});
This will ensure that T2 only runs when T1 comes back. Though, as Ladislav has said the method BuildJourney may not be thread-safe. This will at least get you one step further in determining where your thread failure is. You may want to investigate proper async and multi-threaded debugging. It is a little more complex than simply F5. Sites like pluralsight and the code project have very good tutorials on debugging and writing .net threaded programming.
You have a very good start here. Good luck!
Upvotes: 4