Esempio n. 1
0
 public FileLock 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
                 {
                     ProcessId   = obj["ProcessId"].ToObject <long>(),
                     ProcessName = obj["ProcessName"].ToString(),
                     Timestamp   = obj["Timestamp"].ToObject <long>(),
                     ThreadId    = obj["ThreadId"].ToObject <int>()
                 };
                 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());
     }
 }