예제 #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());
        }
예제 #2
0
        public Boolean ReleaseLock()
        {
            //Need to own the lock in order to release it (and we can reacquire the lock inside the current process)
            if (LockHelper.LockExists(Path) && TryAcquireLock())
            {
                LockHelper.DeleteLock(Path);
            }

            return(true);
        }