public override bool Obtain() { try { if (IsLocked()) { return(false); } using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.Write(_lockFile); return(_remoteDirectory.Upload(stream, _lockFile)); } } return(true); } catch (Exception ex) { Trace.WriteLine($"ERROR {ex.ToString()} Error while obtaining lock"); throw; } }
protected override void Dispose(bool isDisposing) { _fileMutex.WaitOne(); try { var fileName = _name; long originalLength = 0; //this can be null in some odd cases so we need to check if (_indexOutput != null) { try { // make sure it's all written out _indexOutput.Flush(); originalLength = _indexOutput.Length; } finally { _indexOutput.Dispose(); } } if (originalLength > 0) { var result = _azureRemoteDirectory.Upload(CacheDirectory.OpenInput(fileName), fileName, originalLength, _azureSyncDirectory.CompressBlobs, CacheDirectory.FileModified(fileName).ToString()); // push the blobStream up to the cloud if (!result) { throw new Exception("File already exists"); } #if FULLDEBUG Trace.WriteLine($"CLOSED WRITESTREAM {_name}"); #endif } // clean up _indexOutput = null; //_blobContainer = null; GC.SuppressFinalize(this); } finally { _fileMutex.ReleaseMutex(); } }