Esempio n. 1
0
        private bool AcquireLock(int timeoutInMilliseconds)
        {
            try
            {
                lock (_lock)
                {
                    EnsureRoot();
                    IpcMessageLockInfo lockInfo = new IpcMessageLockInfo();
                    bool timeoutExpired         = Exec.TakesTooLong(() =>
                    {
                        bool logged = false;
                        while (File.Exists(LockFile))
                        {
                            if (!logged)
                            {
                                logged = true;
                                IpcMessageLockInfo currentLockInfo = LockFile.FromBinaryFile <IpcMessageLockInfo>();
                                CurrentLockerId          = currentLockInfo?.ProcessId.ToString();
                                CurrentLockerMachineName = currentLockInfo?.MachineName;
                                OnWaitingForLock();
                            }

                            Thread.Sleep(100);
                        }
                        return(LockFile);
                    }, (lockFile) =>
                    {
                        lockInfo.ToBinaryFile(lockFile);
                        return(lockFile);
                    }, TimeSpan.FromMilliseconds(timeoutInMilliseconds));

                    return(!timeoutExpired);
                }
            }
            catch (Exception ex)
            {
                OnAcquireLockException(ex);
                return(false);
            }
        }