I have a thread that loads data when a service is starting. I call a method in this thread that uses Parallel.Foreach to iterate a set of data. But the linq query that I have inside the parallel forerach, gets a objet reference not set to an instance error.
*The same logic works if I remove the Parallel.Foreach though or if I remove the thread. *Even locking the list doesnt help.**
loadingThread = new Thread(new ThreadStart(PreloadData));
loadingThread.IsBackground = true;
loadingThread.Start();
---------------------------------------
public static void PreloadData()
{
Parallel.ForEach(loadedIDs.Keys, indexDefId =>
{
List<FixingEvent> lst = null;
lock (loadedEvents)
{
lst = (from e in loadedEvents where e.DIVACode.Equals(indexDefId) select e).ToList();
}
---------------------------
}
I get an exception in the linq query inside – ‘ e is null and hence object reference error’.
Any help is appreciated.
I guess the list loadedEvents contains null elements. Maybe due to a race condition.