I got a lot of error from a LINQ DataContext, such as "Invalid attempt to call Read when reader is closed" . That is because I was using a singleton for DataContext in a multi-threads.
The codes like that:
Code:
| private static ExampleDataContext Context; |
| private static ExampleDataContext GetContext() |
| { |
| if(Context == null) |
| { |
| Context = new ExampleDataContext(); |
| } |
| return Context; |
| } |
That is not very thread-safe. A thread will close the DataContext, but others threads are still using DataContext. Then it will throw the error above.
I changed the DataContext in a pre-thread. I initialize the DataContext in each thread. The errors are gone! You also can do the DataContext in a pre-object. That is simpler.
Recent comments