tests/nxtlibtesterGUI/Program.cs
/// <summary>
/// The main entry point for the application.
/// </summary>
The main entry point for the application.
The main entry point for the application .
[STAThread]
        static void Main(string[] args)
        {
            string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();
            string mutexId = string.Format("Global\\{{{0}}}", appGuid);
            using (var mutex = new Mutex(false, mutexId))
            {
                var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
                var securitySettings = new MutexSecurity();
                securitySettings.AddAccessRule(allowEveryoneRule);
                mutex.SetAccessControl(securitySettings);
                var hasHandle = false;
                try
                {
                    try
                    {
                        hasHandle = mutex.WaitOne(500, false);
                        if (hasHandle == false)
                        {
                            if (args.Count() == 1)
                            {
                                if (args[0] == "update")
                                {
                                    NamedPipeClientStream pipe = new NamedPipeClientStream(".", "NXTLibTesterGUI_forceupdatepipe", PipeDirection.Out);
                                    StreamWriter sw = new StreamWriter(pipe);
                                    pipe.Connect();
                                    sw.AutoFlush = true;
                                    sw.WriteLine("Force Update Now!");
                                    pipe.WaitForPipeDrain();
                                    pipe.Dispose();
                                }
                                return;
                            }
                            MessageBox.Show("Application already running!  Close the other instance and try again.",
                                   "Application Running", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    catch (AbandonedMutexException)
                    {
                        hasHandle = true;
                    }
                    FindNXT.updatewaiting = false;
                    if (args.Count() == 1)
                    {
                        if (args[0] == "update")
                        {
                            FindNXT.updatewaiting = true;
                        }
                    }
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new FindNXT());
                }
                finally
                {
                    , added if statemnet
                    if (hasHandle)
                        mutex.ReleaseMutex();
                }
            }
        }
[ STAThread ] static void Main ( string [ ] args ) { string appGuid = ( ( GuidAttribute ) Assembly . GetExecutingAssembly ( ) . GetCustomAttributes ( typeof ( GuidAttribute ) , false ) . GetValue ( 0 ) ) . Value . ToString ( ) ; string mutexId = string . Format ( " \\ " , appGuid ) ; using ( var mutex = new Mutex ( false , mutexId ) ) { var allowEveryoneRule = new MutexAccessRule ( new SecurityIdentifier ( WellKnownSidType . WorldSid , null ) , MutexRights . FullControl , AccessControlType . Allow ) ; var securitySettings = new MutexSecurity ( ) ; securitySettings . AddAccessRule ( allowEveryoneRule ) ; mutex . SetAccessControl ( securitySettings ) ; var hasHandle = false ; try { try { hasHandle = mutex . WaitOne ( 500 , false ) ; if ( hasHandle == false ) { if ( args . Count ( ) == 1 ) { if ( args [ 0 ] == " " ) { NamedPipeClientStream pipe = new NamedPipeClientStream ( " " , " " , PipeDirection . Out ) ; StreamWriter sw = new StreamWriter ( pipe ) ; pipe . Connect ( ) ; sw . AutoFlush = true ; sw . WriteLine ( " " ) ; pipe . WaitForPipeDrain ( ) ; pipe . Dispose ( ) ; } return ; } MessageBox . Show ( " " , " " , MessageBoxButtons . OK , MessageBoxIcon . Error ) ; return ; } } catch ( AbandonedMutexException ) { hasHandle = true ; } FindNXT . updatewaiting = false ; if ( args . Count ( ) == 1 ) { if ( args [ 0 ] == " " ) { FindNXT . updatewaiting = true ; } } Application . EnableVisualStyles ( ) ; Application . SetCompatibleTextRenderingDefault ( false ) ; Application . Run ( new FindNXT ( ) ) ; } finally { if ( hasHandle ) mutex . ReleaseMutex ( ) ; } } }
The main entry point for the application.
The main entry point for the application .
// get application GUID as defined in AssemblyInfo.cs // unique id for global mutex - Global prefix means it is global to the machine // edited by Jeremy Wiebe to add example of setting up security for multi-user usage // edited by 'Marc' to work also on localized systems (don't use just "Everyone")  // edited by acidzombie24 // note, you may want to time out here instead of waiting forever // edited by acidzombie24 // mutex.WaitOne(Timeout.Infinite, false); //TODO: send message to pipe // Log the fact the mutex was abandoned in another process, it will still get aquired // Perform your work here. // edited by acidzombie24, added if statemnet
args
string[]