/// <summary> /// Open a file for the specified fileAccess and fileShare, or return null if open fails (avoids exceptions). /// </summary> /// <param name="fullFileNamePath">The full-path file name to open for the specified access.</param> /// <param name="fileAccess">The FileAccess with which to open the file.</param> /// <param name="fileShare">The FileShare to allow to overlap with this open.</param> /// <param name="manualDeleteOnClose">Whether the (successfully-opened) FileLock returned should delete the file /// upon dispose.</param> /// <returns>A disposable FileLock opened with the specified access and sharing), or null if the attempt failed.</returns> private FileLock OpenFileAccess(string fullFileNamePath, FileAccess fileAccess, FileShare fileShare, bool manualDeleteOnClose) { uint flags = 0; FileLock fileOpen = null; FileStream fileStream = null; try { // Make sure we don't thread-abort in the FileStream() ctor. try { } finally { fileStream = new FileStream(fullFileNamePath, FileMode.OpenOrCreate, fileAccess, fileShare, 8192, (FileOptions)flags); } } //catch (ThreadAbortException) //not available in .NET Core 1.1 //{ // if (fileStream != null) // fileStream.Dispose(); // Make sure this gets cleaned up if the requesting thread is aborting! // throw; //} catch { fileStream = null; } if (fileStream != null) { fileOpen = new FileLock(fileStream, fullFileNamePath, FileMode.OpenOrCreate, fileAccess, fileShare, manualDeleteOnClose); } return(fileOpen); }
/// <summary> /// Performs the actual releasing of managed and unmanaged resources. /// Most usage should instead call Dispose(), which will call Dispose(true) for you /// and will suppress redundant finalization. /// </summary> /// <param name="releaseManaged">Indicates whether to release managed resources. /// This should only be called with true, except from the finalizer which should call Dispose(false).</param> private void Dispose(bool releaseManaged) { if (releaseManaged) { // Free managed resources here (normal Dispose() stuff, which should itself call Dispose(true)) // Other objects may be referenced in this case lock (m_QueueLock) { if (!m_Disposed) { m_Disposed = true; // Make sure we don't do it more than once. // Empty our queue (although it should already be empty!). while (m_WaitQueue.Count > 0) { InterprocessLock lockInstance = m_WaitQueue.Dequeue(); //lockInstance.Disposed -= Lock_Disposed; // Suppress the events, don't start new turns! lockInstance.Dispose(); // Tell any threads still waiting that their request has expired. } if (m_CurrentLockTurn == null) { // No thread is currently prepared to do this, so clear them here. if (m_LockRequest != null) { m_LockRequest.Dispose(); m_LockRequest = null; } if (m_FileLock != null) { m_FileLock.Dispose(); m_FileLock = null; } } // We're not fully disposed until the current lock owner gets disposed so we can release the lock. // But fire the event to tell the RepositoryLockManager that we are no longer a valid proxy. OnDispose(); } } } else { // Free native resources here (alloc's, etc) // May be called from within the finalizer, so don't reference other objects here // But we need to make sure the file opens get cleaned up, so we will anyway if we have to.... ??? if (m_LockRequest != null) { m_LockRequest.Dispose(); m_LockRequest = null; } if (m_FileLock != null) { m_FileLock.Dispose(); m_FileLock = null; } } }