| 123456789101112131415161718192021222324252627282930313233 |
- using System;
- using System.Threading;
- namespace BeiJingComService.Infrastructure
- {
- public sealed class SingleInstanceGuard : IDisposable
- {
- private readonly Mutex _mutex;
- private bool _owned;
- public SingleInstanceGuard(string mutexName)
- {
- _mutex = new Mutex(true, mutexName, out _owned);
- }
- public bool IsOwned
- {
- get { return _owned; }
- }
- public void Dispose()
- {
- if (_owned)
- {
- try { _mutex.ReleaseMutex(); }
- catch { }
- _owned = false;
- }
- try { _mutex.Dispose(); }
- catch { }
- }
- }
- }
|