Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:18:28+00:00 2026-06-10T11:18:28+00:00

Been looking for a multi-threading solution for my application, but keep getting random NullReferenceException

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:18:30+00:00Added an answer on June 10, 2026 at 11:18 am

    “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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking into multi-module projects in Maven, and I can only find examples
Ive been looking for a proper rounding mechanism but nothing I find seems to
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
I've been experimenting with multi threading and parallel processing and I needed a counter
I have been looking around if Gzip supports multi-part file compression. From what I
I've been looking over django's multi-db docs. I'd like to break a few of
I've been looking and trying a bunch of different things but I can't solve
Been looking around and can't find a definite way to do this... I need
Been looking around a bit and I can't seem to find any help on
Been looking everywhere, I just can't seen to find it (probably because I'm wording

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.