Ejemplo n.º 1
0
        //Set the IO lock.
        private static bool IOLockSet(SMutex iLock)
        {
            bool blnResult = false;

            try
            {
                if (iLock != null)
                {
                    if (iLock.MutexOwnerThread != Thread.CurrentThread)
                    {
                        blnResult = iLock.Mutex.WaitOne();
                        iLock.IsMutexHeldByCurrentAppDomain = blnResult;
                        if (blnResult)
                        {
                            iLock.MutexOwnerThread = Thread.CurrentThread;
                        }
                        else
                        {
                            throw new ApplicationException("Failed to obtain the IO lock.");
                        }
                    }
                }
            }
            catch (AbandonedMutexException iMutexAbandonedException)
            {
                blnResult = true;
                iLock.IsMutexHeldByCurrentAppDomain = true;
                iLock.MutexOwnerThread = Thread.CurrentThread;
            }
            return(blnResult);
        }
Ejemplo n.º 2
0
        private static SMutex IOMutexGet(string iMutexNameBase)
        {
            SMutex clsResult = null;

            clsResult = new SMutex();
            string strSystemObjectName = @"Global\" + iMutexNameBase.Replace('\\', '_');
            //Give permissions to all authenticated users.
            SecurityIdentifier clsAuthenticatedUsers = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
            MutexSecurity      clsMutexSecurity      = new MutexSecurity();
            MutexAccessRule    clsMutexAccessRule    = new MutexAccessRule(
                clsAuthenticatedUsers,
                MutexRights.FullControl,
                AccessControlType.Allow);

            clsMutexSecurity.AddAccessRule(clsMutexAccessRule);
            //Create the mutex or open an existing one.
            bool blnCreatedNew;

            clsResult.Mutex = new Mutex(
                false,
                strSystemObjectName,
                out blnCreatedNew,
                clsMutexSecurity);
            clsResult.IsMutexHeldByCurrentAppDomain = false;
            return(clsResult);
        }
Ejemplo n.º 3
0
        //Set the IO lock.
        private static bool IOLockSet(SMutex iLock)
        {
            bool blnResult = false;

            try
            {
                if (iLock != null)
                {
                    if (iLock.MutexOwnerThread != Thread.CurrentThread)
                    {
                        blnResult = iLock.Mutex.WaitOne();
                        iLock.IsMutexHeldByCurrentAppDomain = blnResult;
                        if (blnResult)
                        {
                            iLock.MutexOwnerThread = Thread.CurrentThread;
                        }
                    }
                    if (!blnResult)
                    {
                        throw new ApplicationException("Не удалось получить монопольный доступ к теневой копии сборок.");
                    }
                }
            }
            catch (AbandonedMutexException iMutexAbandonedException)
            {
                blnResult = true;
                iLock.IsMutexHeldByCurrentAppDomain = true;
                iLock.MutexOwnerThread = Thread.CurrentThread;
            }
            return(blnResult);
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            string strEnumStorageFilePath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                "MyAppEnumStorage.txt");

            mclsIOLock = IOMutexGet(strEnumStorageFilePath);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            //Initialize the mutex. Here you need to know the path to the file you use to store application data.
            string strEnumStorageFilePath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                "MyAppEnumStorage.txt");

            mclsIOLock = IOMutexGet(strEnumStorageFilePath);
        }
Ejemplo n.º 6
0
 //Release IO lock.
 private static void IOLockRelease(
     SMutex iLock,
     bool?iLockIsSetInCurrentStackFrame = null)
 {
     if (iLock != null)
     {
         lock (iLock)
         {
             if (iLock.IsMutexHeldByCurrentAppDomain &&
                 (!iLockIsSetInCurrentStackFrame.HasValue ||
                  iLockIsSetInCurrentStackFrame.Value))
             {
                 iLock.MutexOwnerThread = null;
                 iLock.IsMutexHeldByCurrentAppDomain = false;
                 iLock.Mutex.ReleaseMutex();
             }
         }
     }
 }