예제 #1
0
        public bool TestLockIsFree()
        {
            if (LockIO.LockExists(LockFilePath))
            {
                var lockContent = LockIO.ReadLock(LockFilePath);

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

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

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

                // Check if PID is still alive
                if (Process.GetProcesses().Any(x => x.Id == lockContent.PID))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #2
0
        public bool TryAcquireLock()
        {
            if (LockIO.LockExists(LockFilePath))
            {
                var lockContent = LockIO.ReadLock(LockFilePath);

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

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

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

                //The process which created it is no longer running
                if (!ProcessIsRunning((int)lockContent.PID, lockContent.ProcessName))
                {
                    return(AcquireLock());
                }

                if (LockTimeout != TimeSpan.Zero)
                {
                    var lockWriteTime = new DateTime(lockContent.Timestamp);
                    if (!(Math.Abs((DateTime.Now - lockWriteTime).TotalSeconds) > LockTimeout.TotalSeconds))
                    {
                        return(false);                        //The lock has not timed out - we can't acquire it
                    }
                }
                else
                {
                    return(false);
                }
            }

            //Acquire the lock

            return(AcquireLock());
        }
예제 #3
0
        public bool TryAcquireLock()
        {
            if (LockIO.LockExists(LockFilePath))
            {
                var lockContent = LockIO.ReadLock(LockFilePath);

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

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


                var lockWriteTime = new DateTime(lockContent.Timestamp);

                //This lock belongs to this process - we can reacquire the lock
                var currentProcess = Process.GetCurrentProcess();
                if (lockContent.PID == currentProcess.Id && lockContent.MachineName == Environment.MachineName)
                {
                    return(AcquireLock());
                }

                //The lock has not timed out - we can't acquire it
                if (!(Math.Abs((DateTime.Now - lockWriteTime).TotalSeconds) > LockTimeout.TotalSeconds))
                {
                    return(false);
                }
            }

            //Acquire the lock

            return(AcquireLock());
        }