Beispiel #1
0
        public Boolean TryAcquireLock()
        {
            if (!LockHelper.LockExists(Path))
            {
                return(AcquireLock());
            }

            FileLockContent content = LockHelper.ReadLock(Path);

            //Someone else owns the lock
            if (content.GetType() == typeof(OtherProcessOwnsFileLockContent))
            {
                return(false);
            }

            //the file no longer exists
            if (content.GetType() == typeof(MissingFileLockContent))
            {
                return(AcquireLock());
            }

            DateTime writeTime = new DateTime(content.Timestamp);

            //This lock belongs to this process - we can reacquire the lock
            if (content.PID == Process.GetCurrentProcess().Id)
            {
                return(AcquireLock());
            }

            //The lock has not timed out - we can't acquire it
            return(Math.Abs((DateTime.Now - writeTime).TotalSeconds) > Timeout.TotalSeconds && AcquireLock());
        }
Beispiel #2
0
 public static bool WriteLock(string lockFilePath, FileLockContent lockContent)
 {
     try
     {
         using (var stream = File.Create(lockFilePath))
         {
             JsonSerializer.WriteObject(stream, lockContent);
         }
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Beispiel #3
0
		public static bool WriteLock(string lockFilePath, FileLockContent lockContent)
		{
			try
			{
				using (var stream = File.Create(lockFilePath))
				{
					JsonSerializer.WriteObject(stream, lockContent);
				}
				return true;
			}
			catch (Exception)
			{
				return false;
			}
		}
Beispiel #4
0
        public static Boolean WriteLock(String path, FileLockContent content)
        {
            try
            {
                using FileStream stream = File.Create(path);

                JsonSerializer.WriteObject(stream, content);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Beispiel #5
0
 public static bool WriteLock(string lockFilePath, FileLockContent lockContent)
 {
     try
     {
         var obj = new JObject();
         // type is only required for forward compatibility.
         // older versions of this library used DataContractJsonSerializer to read/write the lock file contents.
         // by writing out the old type value, it allows older versions of this library to continue to be able
         // to read the lock files correctly.
         obj["__type"]      = "FileLockContent:#Palaso.IO.FileLock";
         obj["PID"]         = lockContent.PID;
         obj["ProcessName"] = lockContent.ProcessName;
         obj["Timestamp"]   = lockContent.Timestamp;
         using (StreamWriter streamWriter = new StreamWriter(File.Create(lockFilePath)))
         {
             obj.WriteTo(new JsonTextWriter(streamWriter));
         }
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Beispiel #6
0
		public static bool WriteLock(string lockFilePath, FileLockContent lockContent)
		{
			try
			{
				var obj = new JObject();
				// type is only required for forward compatibility.
				// older versions of this library used DataContractJsonSerializer to read/write the lock file contents.
				// by writing out the old type value, it allows older versions of this library to continue to be able
				// to read the lock files correctly.
				obj["__type"] = "FileLockContent:#Palaso.IO.FileLock";
				obj["PID"] = lockContent.PID;
				obj["ProcessName"] = lockContent.ProcessName;
				obj["Timestamp"] = lockContent.Timestamp;
				using (StreamWriter streamWriter = new StreamWriter(File.Create(lockFilePath)))
				{
					obj.WriteTo(new JsonTextWriter(streamWriter));
				}
				return true;
			}
			catch (Exception)
			{
				return false;
			}
		}