Reputation: 7269
Just a general question, I'm developing an ASP.NET MVC 3 web application that reads from a configuration file using the Stream Reader inside a Using statement for automatic disposal as follows:
using (StreamReader sr = new StreamReader(filepath))
{
}
My concern is that when multiple users are running the application, a deadlock will occur when multiple instances of the application are reading values from the configuration file. My question is, are deadlocks an issue when working with the Stream Reader class? Intuitively I would think not, since I'm assuming the Stream Reader is opening the file in read-only mode and there are no external processes writing to the configuration file other than manual edits. I just wanted to be sure whether or not this would be an issue. Any insight is appreciated.
Upvotes: 5
Views: 748
Reputation: 1038840
My concern is that when multiple users are running the application, a deadlock will occur when multiple instances of the application are reading values from the configuration file
No, don't worry. No deadlock will occur. You are opening the file for reading. So you could have concurrent readers. But if you write to this file in some other location of your code that you will get an exception. But no deadlock. If you want to properly synchronize readers and writers to some shared resource such as a file you could use the ReaderWriterLockSlim class. If you never write to the file (from the same process or some other process) then you are fine. But if you never write to the file, to avoid reading and parsing this file everytime, you should only read it once and cache it in some memory structure. For example that's how the .NET application configuration system works. It reads and parses the app/web.config once when running the application and then stores it into memory for faster access. But of course everything will depend on your exact scenario.
Upvotes: 8