locked
Make app launch only once - Single Instance (WolfKC) - 7/20/2006 4:36 PM PST RRS feed

  • Question

  • By: WolfKC

    I tried adding a Mutex in the Oninitialized function to stop the app from 
    loading if another copy was already running, but it doesn't work. 
    I'm essentially using the code at 
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=453945&SiteID=1
    but I'm exiting if the mutex fails to create. The problem is the mutex never 
    fails even if the exact app is already running. Does the 2nd parm after the 
    new mutex have to be something special? I'm thinking the issue really has 
    somethin to do with when the test is being run?
    Tuesday, February 19, 2008 1:37 AM

Answers

  • By: Brennon Williams

    Howdy Kerry...
    
    That is a bit of mess that code that you are looking at... try this if you 
    are up for it...
    
    Your application Main() method usually looks like this:
    
            [System.STAThreadAttribute()]
            public static void Main() {
                WindowsApplication1.App app = new WindowsApplication1.App();
                app.InitializeComponent();
                app.Run();
            }
    
    
    You want to try it like this:
    
            [System.STAThreadAttribute()]
            public static void Main() {
                WindowsApplication1.App app = new WindowsApplication1.App();
    
                if (RunningInstance)
                {
                    app.InitializeComponent();
                    app.Run();
                }
                else
                {
                    MessageBox.Show("Already Running");
                }
            }
    
           
            static bool RunningInstance;
            static System.Threading.Mutex mutex = new 
    System.Threading.Mutex(false, "Local\\MyAppName", out RunningInstance);
    
    
    
    Dont forget the stuff at the bottom...
    
    This works as I use it my self... Let me know if you have any problems
    
    cheers
    
    "WolfKC" wrote:
    
    > I tried adding a Mutex in the Oninitialized function to stop the app from 
    > loading if another copy was already running, but it doesn't work. 
    > I'm essentially using the code at 
    > http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=453945&SiteID=1
    > but I'm exiting if the mutex fails to create. The problem is the mutex never 
    > fails even if the exact app is already running. Does the 2nd parm after the 
    > new mutex have to be something special? I'm thinking the issue really has 
    > somethin to do with when the test is being run?
    Tuesday, February 19, 2008 7:04 PM

All replies

  • By: Brennon Williams

    Howdy Kerry...
    
    That is a bit of mess that code that you are looking at... try this if you 
    are up for it...
    
    Your application Main() method usually looks like this:
    
            [System.STAThreadAttribute()]
            public static void Main() {
                WindowsApplication1.App app = new WindowsApplication1.App();
                app.InitializeComponent();
                app.Run();
            }
    
    
    You want to try it like this:
    
            [System.STAThreadAttribute()]
            public static void Main() {
                WindowsApplication1.App app = new WindowsApplication1.App();
    
                if (RunningInstance)
                {
                    app.InitializeComponent();
                    app.Run();
                }
                else
                {
                    MessageBox.Show("Already Running");
                }
            }
    
           
            static bool RunningInstance;
            static System.Threading.Mutex mutex = new 
    System.Threading.Mutex(false, "Local\\MyAppName", out RunningInstance);
    
    
    
    Dont forget the stuff at the bottom...
    
    This works as I use it my self... Let me know if you have any problems
    
    cheers
    
    "WolfKC" wrote:
    
    > I tried adding a Mutex in the Oninitialized function to stop the app from 
    > loading if another copy was already running, but it doesn't work. 
    > I'm essentially using the code at 
    > http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=453945&SiteID=1
    > but I'm exiting if the mutex fails to create. The problem is the mutex never 
    > fails even if the exact app is already running. Does the 2nd parm after the 
    > new mutex have to be something special? I'm thinking the issue really has 
    > somethin to do with when the test is being run?
    Tuesday, February 19, 2008 7:04 PM
  • By: WolfKC

    One of the irritants I have with EID is that it seems the main() is generated 
    on the fly, due to xaml. I think there is a way to tell xaml to launch 
    something else and you can make main not be programitically generated, but I 
    dont want to get into that now if I dont really need to.
    So anyway, I put the bool and mutex in my window class and put the check in 
    my public window1() around the existing this.InitalizeComponents(), and if it 
    fails I do Environment.Exit(0); .... And it seems to work. :)
    
    Thanks!
    
    "Brennon Williams" wrote:
    
    > Howdy Kerry...
    > 
    > That is a bit of mess that code that you are looking at... try this if you 
    > are up for it...
    > 
    > Your application Main() method usually looks like this:
    > 
    >         [System.STAThreadAttribute()]
    >         public static void Main() {
    >             WindowsApplication1.App app = new WindowsApplication1.App();
    >             app.InitializeComponent();
    >             app.Run();
    >         }
    > 
    > 
    > You want to try it like this:
    > 
    >         [System.STAThreadAttribute()]
    >         public static void Main() {
    >             WindowsApplication1.App app = new WindowsApplication1.App();
    > 
    >             if (RunningInstance)
    >             {
    >                 app.InitializeComponent();
    >                 app.Run();
    >             }
    >             else
    >             {
    >                 MessageBox.Show("Already Running");
    >             }
    >         }
    > 
    >        
    >         static bool RunningInstance;
    >         static System.Threading.Mutex mutex = new 
    > System.Threading.Mutex(false, "Local\\MyAppName", out RunningInstance);
    > 
    > 
    > 
    > Dont forget the stuff at the bottom...
    > 
    > This works as I use it my self... Let me know if you have any problems
    > 
    > cheers
    > 
    > "WolfKC" wrote:
    > 
    > > I tried adding a Mutex in the Oninitialized function to stop the app from 
    > > loading if another copy was already running, but it doesn't work. 
    > > I'm essentially using the code at 
    > > http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=453945&SiteID=1
    > > but I'm exiting if the mutex fails to create. The problem is the mutex never 
    > > fails even if the exact app is already running. Does the 2nd parm after the 
    > > new mutex have to be something special? I'm thinking the issue really has 
    > > somethin to do with when the test is being run?
    Tuesday, February 19, 2008 7:04 PM