void Unlock()
 {
     if (currentAppLock != null)
     {
         currentAppLock.Dispose();
         currentAppLock = null;
     }
 }
Пример #2
0
        public void Dispose()
        {
            // this is necessary because System.Data.Sqlite does not release handle on SQLiteConnection Dispose, only on object finalization.
            GC.Collect();
            GC.WaitForPendingFinalizers();

            fileLock.Dispose();
            disposed = true;
        }
Пример #3
0
 public static void TestFileLockDoubleDispose()
 {
     Assert.DoesNotThrow(() =>
     {
         using (FileLock aLock = FileLock.Lock(OS.Current.FileInfo(_fileExtPath)))
         {
             aLock.Dispose();
         }
     });
 }
Пример #4
0
 public static void TestFileLockDoubleDispose()
 {
     Assert.DoesNotThrow(() =>
     {
         using (FileLock aLock = New <FileLocker>().Acquire(New <IDataStore>(_fileExtPath)))
         {
             aLock.Dispose();
         }
     });
 }
Пример #5
0
 public void ReleaseLock()
 {
     if (entered)
     {
         try
         {
             fileLock.Dispose();
         }
         finally
         {
             entered = false;
         }
     }
 }
Пример #6
0
            public void LocksAndReleases()
            {
                var lockFilePath = Path.Combine(TempDir.AbsolutePath, LockFileName);

                AssertNoFile(lockFilePath);

                FileLock = FileLock.EnterWithCreateWait(lockFilePath, TimeSpan.Zero);

                AssertLockFails(lockFilePath);

                FileLock.Dispose();

                AssertNoLock(lockFilePath);
            }
        /// <inheritdoc />
        public IDisposable GetLock(string name)
        {
            FileLock fileLock = null;

            try
            {
                Directory.CreateDirectory(_path);
            }
            catch (UnauthorizedAccessException ex)
            {
                GC.KeepAlive(ex);
#if DEBUG
                Trace.TraceWarning("Unable to create directory to index path, locking will not be feasible.  Exception: {0}", ex);
#endif
                throw; // we aren't going to try to spinlock on this.. we failed.
            }
            catch (Exception ex)
            {
                GC.KeepAlive(ex);
#if DEBUG
                Trace.TraceWarning("Unable to create directory to index path, locking will not be feasible.  Exception: {0}", ex);
#endif
            }

            var lockFullFileNamePath = GetLockFileName(_path, name);

            try
            {
                // We share Read so that other processes who desire the lock can open for read to signal that to us.
                // (Except on Mono we have to use a separate file for the request signal, but this is still okay for the lock.)
                fileLock = OpenFileAccess(lockFullFileNamePath, FileAccess.Write, FileShare.Read, _deleteOnClose);
            }
            catch (ThreadAbortException)
            {
                if (fileLock != null)
                {
                    fileLock.Dispose(); // Make sure it's cleaned up if the requesting thread is aborting!
                }
                throw;
            }
            catch
            {
                //don't care why we failed, we just did - so no lock for you!
                fileLock = null;
            }

            return(fileLock);
        }
Пример #8
0
        public async void Dispose()
        {
            var   file     = new FileInfo(Path.GetTempFileName());
            ILock fileLock = new FileLock(file);

            if (await fileLock.TryAcquire(TimeSpan.FromHours(1)))
            {
                using (fileLock)
                {
                    Assert.True(File.Exists(Path.ChangeExtension(file.FullName, Extension)));
                    fileLock.Dispose();
                }
            }

            Assert.False(File.Exists(Path.ChangeExtension(file.FullName, Extension)));
        }
Пример #9
0
        public void TryLock_ExistingFile_NotLocked()
        {
            var dir = Path.Combine(OutputDir, "ExistingFile_NotLocked");

            Directory.CreateDirectory(dir);
            var fileLock = new FileLock(dir);

            File.Create(fileLock.LockFile).Close();
            Assert.IsTrue(File.Exists(fileLock.LockFile));
            var locked = fileLock.TryLock();

            Assert.IsTrue(locked);
            Assert.IsTrue(File.Exists(fileLock.LockFile));
            fileLock.Dispose();
            Assert.IsFalse(File.Exists(fileLock.LockFile));
        }
Пример #10
0
        public void TryLock_NoLock()
        {
            var dir      = Path.Combine(OutputDir, "NoLock");
            var fileLock = new FileLock(dir);

            if (File.Exists(fileLock.LockFile))
            {
                File.Delete(fileLock.LockFile);
            }
            Assert.IsFalse(File.Exists(fileLock.LockFile));
            var locked = fileLock.TryLock();

            Assert.IsTrue(locked);
            Assert.IsTrue(File.Exists(fileLock.LockFile));
            fileLock.Dispose();
            Assert.IsFalse(File.Exists(fileLock.LockFile));
        }
Пример #11
0
 public void Dispose()
 {
     fileLock.Dispose();
     disposed = true;
 }
Пример #12
0
        public void AddStageEntry([NotNull] StageEntry se, [NotNull] RunningConfig config)
        {
            if (!config.MakeStageEntries)
            {
                return;
            }

            Console.WriteLine("Config in stage: " + JsonConvert.SerializeObject(config, Formatting.Indented));
            if (!string.IsNullOrWhiteSpace(se.DevelopmentStatus))
            {
                se.ImplementationFinished = false;
            }

            // ReSharper disable once InconsistentlySynchronizedField
            string stagePath = Path.Combine(_config.Directories.BaseProcessingDirectory, "stages.xlsx");
            string lockPath  = stagePath + ".lock";
            Random rnd       = new Random();

            try {
                using (ILock fileLock = new FileLock(lockPath)) {
                    try {
                        bool lockAquired = false;
                        while (!lockAquired)
                        {
                            try {
                                fileLock.TryAcquire(TimeSpan.FromSeconds(5));
                                lockAquired = true;
                            }
#pragma warning disable CA1031 // Do not catch general exception types
                            catch (Exception ex) {
#pragma warning restore CA1031 // Do not catch general exception types
                                Console.WriteLine("Locking failed for file: " + stagePath + ": " + ex.Message);
                                Thread.Sleep(rnd.Next(500));
                            }
                        }

                        WriteXlsFileContent(stagePath, se);
                    }
#pragma warning disable CA1031 // Do not catch general exception types
                    catch (Exception ex1) {
#pragma warning restore CA1031 // Do not catch general exception types
                        Console.WriteLine("trying to log result file, exception : " + ex1.Message);
                    }
                    finally {
                        try {
                            fileLock.Dispose();
                        }
#pragma warning disable CA1031 // Do not catch general exception types
                        catch (Exception x) {
#pragma warning restore CA1031 // Do not catch general exception types
                            Console.WriteLine("failed to release lock, exception : " + x.Message);
                        }
                    }
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex) {
#pragma warning restore CA1031 // Do not catch general exception types
#pragma warning restore CA1031 // Do not catch general exception types
                Console.WriteLine("Locking release failed for file: " + stagePath + ": " + ex.Message);
                Thread.Sleep(rnd.Next(500));
            }
        }