Beispiel #1
0
        static void Main(string[] args)
        {
            // We're a single instance application!
            // http://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c/229567

            string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(
                                  typeof(GuidAttribute), false).GetValue(0)).Value.ToString();

            // Global prefix means that the mutex is global to the machine
            string mutexId = string.Format("Global\\{{{0}}}", appGuid);

            bool createdNew;

            var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                                                        MutexRights.FullControl, AccessControlType.Allow);
            var securitySettings = new MutexSecurity();

            securitySettings.AddAccessRule(allowEveryoneRule);

            using (var mutex = new Mutex(false, mutexId, out createdNew, securitySettings))
            {
                var hasHandle = false;
                try
                {
                    try
                    {
                        hasHandle = mutex.WaitOne(8000, false);
                        if (hasHandle == false)
                        {
                            throw new TimeoutException("Timeout waiting for exclusive access");
                        }
                    }
                    catch (AbandonedMutexException)
                    {
                        hasHandle = true;
                    }
                    catch (TimeoutException)
                    {
                        return;
                    }

                    // Proceed to client startup
                    PreStartup.Initialize(args);
                }
                finally
                {
                    if (hasHandle)
                    {
                        mutex.ReleaseMutex();
                    }
                }
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            bool          noAudio = false;
            bool          multipleInstanceMode = false;
            List <string> unknownStartupParams = new List <string>();

            for (int arg = 0; arg < args.Length; arg++)
            {
                string argument = args[arg].ToUpper();

                switch (argument)
                {
                case "-NOAUDIO":
                    // TODO fix
                    throw new NotImplementedException("-NOAUDIO is currently not implemented, please run the client without it.");

                case "-MULTIPLEINSTANCE":
                    multipleInstanceMode = true;
                    break;

                default:
                    unknownStartupParams.Add(argument);
                    break;
                }
            }

            StartupParams parameters = new StartupParams(noAudio, multipleInstanceMode, unknownStartupParams);

            if (multipleInstanceMode)
            {
                // Proceed to client startup
                PreStartup.Initialize(parameters);
                return;
            }

            // We're a single instance application!
            // http://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c/229567

            string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(
                                  typeof(GuidAttribute), false).GetValue(0)).Value.ToString();

            // Global prefix means that the mutex is global to the machine
            string mutexId = string.Format("Global\\{{{0}}}", appGuid);


            var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                                                        MutexRights.FullControl, AccessControlType.Allow);
            var securitySettings = new MutexSecurity();

            securitySettings.AddAccessRule(allowEveryoneRule);

            using (var mutex = new Mutex(false, mutexId, out bool createdNew, securitySettings))
            {
                var hasHandle = false;
                try
                {
                    try
                    {
                        hasHandle = mutex.WaitOne(8000, false);
                        if (hasHandle == false)
                        {
                            throw new TimeoutException("Timeout waiting for exclusive access");
                        }
                    }
                    catch (AbandonedMutexException)
                    {
                        hasHandle = true;
                    }
                    catch (TimeoutException)
                    {
                        return;
                    }

                    // Proceed to client startup
                    PreStartup.Initialize(parameters);
                }
                finally
                {
                    if (hasHandle)
                    {
                        mutex.ReleaseMutex();
                    }
                }
            }
        }