I'm using concurrent dictionary to hold open files.
To open a new file, I do this:
myDictionary.GetOrAdd (fName, (fn) => new StreamWriter(fn, true));
And with this, I regularly get following exception:
System.IO.IOException: The process cannot access the file '....' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPa
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path, Boolean append)
To me that means that the operation is not atomic and that the software is trying to open the same file twice.
Is there any other operation I can use instead, which will be atomic? I want to avoid locking because of performance considerations.