protected FileInfoLock LockFile(DriveService.Stream stream, string fileId, string filePath) { try { Mutex.Wait(); try { var fileInfoLock = new FileInfoLock(this, fileId, filePath); Items.Add(new Item(fileInfoLock, stream)); if (OnLockFile != null) { OnLockFile(this, fileInfoLock); } return(fileInfoLock); } finally { Mutex.Release(); } } catch (Exception exception) { Log.Error(exception, true, false); return(null); } }
public void UnlockFile(FileInfoLock fileInfoLock) { try { Mutex.Wait(); try { if (fileInfoLock.FileStream != null) { fileInfoLock.FileStream.Dispose(); fileInfoLock.FileStream = null; } int index = 0; bool found = false; foreach (Item item in Items) { if (item.FileInfoLock == fileInfoLock) { found = true; Items.RemoveAt(index); break; } index++; } if (!found) { return; } if (OnUnlockFile != null) { OnUnlockFile(this, fileInfoLock); } } finally { Mutex.Release(); } } catch (Exception exception) { Log.Error(exception); } }
public Item(FileInfoLock fileInfoLock, DriveService.Stream stream) { FileInfoLock = fileInfoLock; Stream = stream; }
protected FileInfoLock LockFile(DriveService.Stream stream, string fileId, string filePath, System.IO.FileMode fileMode, System.IO.FileAccess fileAccess, System.IO.FileShare fileShare) { try { Mutex.Wait(); try { System.IO.FileStream fileStream = null; Exception lastException = null; for (int i = 0; i < 10; i++) { try { // Open file will try and execute for 1 sec, // for this function we will wait up to 10 sec fileStream = OpenFile(filePath, fileMode, fileAccess, fileShare); lastException = null; break; } catch (Exception exception) { lastException = exception; } System.Threading.Thread.Sleep(100); } if (lastException != null) { throw lastException; } FileInfoLock fileInfoLock = null; try { fileInfoLock = LockFile(stream, fileId, filePath); fileInfoLock.FileStream = fileStream; } catch (Exception exception) { if (fileStream != null) { fileStream.Dispose(); fileStream = null; } throw exception; } return(fileInfoLock); } finally { Mutex.Release(); } } catch (Exception exception) { Log.Error(exception, true, false); return(null); } }
public Item(FileInfoLock fileInfoLock, DriveService.Stream stream) { FileInfoLock = fileInfoLock; Stream = stream; }