Example #1
0
 public IFileLock ReadLock(string lockFilePath)
 {
     try
     {
         using (var stream = fileSystem.OpenFileExclusively(lockFilePath, FileMode.Open, FileAccess.Read))
         {
             using (var streamReader = new StreamReader(stream))
             {
                 var obj         = JObject.Load(new JsonTextReader(streamReader));
                 var lockContent = new FileLock(obj["ProcessId"].ToObject <long>(),
                                                obj["ProcessName"].ToString(),
                                                obj["ThreadId"].ToObject <int>(),
                                                obj["Timestamp"].ToObject <long>());
                 if (lockContent.BelongsToCurrentProcessAndThread())
                 {
                     return(lockContent);
                 }
                 return(new OtherProcessOwnsFileLock(lockContent));
             }
         }
     }
     catch (FileNotFoundException)
     {
         return(new MissingFileLock());
     }
     catch (IOException)
     {
         return(new OtherProcessHasExclusiveLockOnFileLock());
     }
     catch (JsonReaderException)
     {
         return(new UnableToDeserialiseLockFile(fileSystem.GetCreationTime(lockFilePath)));
     }
     catch (Exception) //We have no idea what went wrong - reacquire this lock
     {
         return(new OtherProcessHasExclusiveLockOnFileLock());
     }
 }
Example #2
0
 public OtherProcessOwnsFileLock(FileLock fileLock) : base(fileLock.ProcessId, fileLock.ProcessName, fileLock.ThreadId, fileLock.Timestamp)
 {
 }
Example #3
0
 protected bool Equals(FileLock other)
 {
     return(ProcessId == other.ProcessId &&
            string.Equals(ProcessName, other.ProcessName) &&
            ThreadId == other.ThreadId);
 }