There’s a code that reads a file and makes some calculating operations with its content in another thread using the classes Task and StreamReader.
Task t= new Task(() => DoSomeWork(myFile));
t.Start();
But inspite of the file is immutable sometimes I’m getting the different results! Why? What should I do to resolve it?
You’ve clearly got something inside DoSomeWork() that is not thread safe. The most likely candidate for this is some static code, since that would mean that there’s only one copy of that particular piece of code in memory. One instance of DoSomeWork() could get context switched out, and another instance would then pick up on the state of the static method, giving you some unpredictable results.
Other things could include improper use of dependency injection, a singleton object that’s being shared, certain libraries being improperly used as asynchronous, or a couple other things – this is what Drew was talking about when he asked for more information.