Exemplo n.º 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());
        }