Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. SingleInstance Application

SingleInstance Application

Scheduled Pinned Locked Moved WPF
csharpwpfcomtutorial
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jammer 0

    Hi All, I need to make an application single instance, I've read and seen a few examples of doing this but none have really touched on WHERE to do this. In a WPF application with a normal App class inheriting from Application is this the best place to perform this check? I'm looking at the Adam Nathan book which has a code sample, but putting this into the App class doesn't seem to work (this wont compile!):

    public partial class App : Application
    {
        bool mutexIsNew;
        using (System.Threading.Mutex m = new System.Threading.Mutex(true, "uniquename", out mutexIsNew))
        {
            if(mutexIsNew)
            // new instance
            else
            //shutdown
        }
    }
    

    Looking at this MS example http://msdn.microsoft.com/en-us/library/ms771662.aspx[^] rather than using a mutex (which seems the best way) they import a visual basic namespace into a csharp app!! Cheers,

    Jammer Going where everyone here has gone before! :) My Blog

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #2

    You need to change the startup class so that it points to the one that has your Main method. This is accomplished by changing the Properties for your application in VS solution explorer. Look for the Startup Object list and choose your class. Note that you normally don't need to do this because VS automatically creates the Main based on the XAML app template. So, your main class would look something like this:

    public class MyApp : Application
    {
    private static bool newMutex;
    public static void Main()
    {
    using (Mutex m = new Mutex(true, "myApplication", out newMutex))
    {
    if (!mutexIsNew)
    // Shut it down
    }
    }
    }

    Deja View - the feeling that you've seen this post before.

    My blog | My articles

    J 2 Replies Last reply
    0
    • P Pete OHanlon

      You need to change the startup class so that it points to the one that has your Main method. This is accomplished by changing the Properties for your application in VS solution explorer. Look for the Startup Object list and choose your class. Note that you normally don't need to do this because VS automatically creates the Main based on the XAML app template. So, your main class would look something like this:

      public class MyApp : Application
      {
      private static bool newMutex;
      public static void Main()
      {
      using (Mutex m = new Mutex(true, "myApplication", out newMutex))
      {
      if (!mutexIsNew)
      // Shut it down
      }
      }
      }

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      J Offline
      J Offline
      Jammer 0
      wrote on last edited by
      #3

      Hi Pete, Thanks for this. I just popped off and made a StartUpObject.cs file and popped this code in there, then I changed the properties as you describe and the rest of my Presentation Layer UI stuff is blowing up. Popping this code into the App.xaml.cs is telling me that there is already a Main with the same parameter types. Should I be using the 'normal' App.xaml.cs or should I be creating a XAML file to go with my new StartUpObject.cs file? EDIT: Ok, I found some more info on the net and have done this but i'm now seeing binding errors so this isn't a good solution either: Removed the StartUpUri="" from App.xaml and added this to my App.xaml.cs:

      public partial class App : Application
      {
          private static bool mutexIsNew;
      
          public App() : base()
          {
              using (Mutex m = new Mutex(true, "MyApp", out mutexIsNew))
              {
                  if (mutexIsNew)
                  {
                      AppMainWindow mainwindow = new AppMainWindow();
                      mainwindow.Show();
                  }
                  else
                  {
                      App.Current.Shutdown();
                  }
              }
          }
      }
      

      Sorry for not getting this yet pete ... Cheers,

      Jammer Going where everyone here has gone before! :) My Blog

      modified on Friday, June 20, 2008 6:37 PM

      P 1 Reply Last reply
      0
      • P Pete OHanlon

        You need to change the startup class so that it points to the one that has your Main method. This is accomplished by changing the Properties for your application in VS solution explorer. Look for the Startup Object list and choose your class. Note that you normally don't need to do this because VS automatically creates the Main based on the XAML app template. So, your main class would look something like this:

        public class MyApp : Application
        {
        private static bool newMutex;
        public static void Main()
        {
        using (Mutex m = new Mutex(true, "myApplication", out newMutex))
        {
        if (!mutexIsNew)
        // Shut it down
        }
        }
        }

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        J Offline
        J Offline
        Jammer 0
        wrote on last edited by
        #4

        Have you seen this Pete? http://forums.msdn.microsoft.com/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60/[^]

        Jammer Going where everyone here has gone before! :) My Blog

        1 Reply Last reply
        0
        • J Jammer 0

          Hi Pete, Thanks for this. I just popped off and made a StartUpObject.cs file and popped this code in there, then I changed the properties as you describe and the rest of my Presentation Layer UI stuff is blowing up. Popping this code into the App.xaml.cs is telling me that there is already a Main with the same parameter types. Should I be using the 'normal' App.xaml.cs or should I be creating a XAML file to go with my new StartUpObject.cs file? EDIT: Ok, I found some more info on the net and have done this but i'm now seeing binding errors so this isn't a good solution either: Removed the StartUpUri="" from App.xaml and added this to my App.xaml.cs:

          public partial class App : Application
          {
              private static bool mutexIsNew;
          
              public App() : base()
              {
                  using (Mutex m = new Mutex(true, "MyApp", out mutexIsNew))
                  {
                      if (mutexIsNew)
                      {
                          AppMainWindow mainwindow = new AppMainWindow();
                          mainwindow.Show();
                      }
                      else
                      {
                          App.Current.Shutdown();
                      }
                  }
              }
          }
          

          Sorry for not getting this yet pete ... Cheers,

          Jammer Going where everyone here has gone before! :) My Blog

          modified on Friday, June 20, 2008 6:37 PM

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #5

          If you can wait till Monday (when I'm back in the office), I can send you the startup code that we use - I'll drop it into a sample app for you.

          Deja View - the feeling that you've seen this post before.

          My blog | My articles

          J 1 Reply Last reply
          0
          • P Pete OHanlon

            If you can wait till Monday (when I'm back in the office), I can send you the startup code that we use - I'll drop it into a sample app for you.

            Deja View - the feeling that you've seen this post before.

            My blog | My articles

            J Offline
            J Offline
            Jammer 0
            wrote on last edited by
            #6

            That would be awesome Pete. Thank you!

            Jammer Going where everyone here has gone before! :) My Blog

            P 1 Reply Last reply
            0
            • J Jammer 0

              That would be awesome Pete. Thank you!

              Jammer Going where everyone here has gone before! :) My Blog

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #7

              Jammer - here's a quick sample for you to try out:

              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Text;
              using System.Windows;
              using System.Threading;

              namespace SingleInstanceApp
              {
              public class SIApplication : Application
              {
              private static bool isNew;
              [STAThread]
              public static void Main()
              {
              using (Mutex m = new Mutex(true, "SIApplication.Mutex", out isNew))
              {
              if (isNew)
              {
              SIApplication app = new SIApplication();
              app.InitializeComponent();
              app.Run();
              }
              }
              }

              public void InitializeComponent()
              {
                this.StartupUri = new Uri("Window1.xaml", System.UriKind.Relative);
              }
              

              }
              }

              Predictably, you need to set the startup object to point to this class. The trick is that you physically aren't going to create any other instances of the app because you have detected the state of the mutex before you create an instance of the app. BTW - you really need to use the StartupUri, I suspect this is where you've been getting your problems.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              J 1 Reply Last reply
              0
              • P Pete OHanlon

                Jammer - here's a quick sample for you to try out:

                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;
                using System.Windows;
                using System.Threading;

                namespace SingleInstanceApp
                {
                public class SIApplication : Application
                {
                private static bool isNew;
                [STAThread]
                public static void Main()
                {
                using (Mutex m = new Mutex(true, "SIApplication.Mutex", out isNew))
                {
                if (isNew)
                {
                SIApplication app = new SIApplication();
                app.InitializeComponent();
                app.Run();
                }
                }
                }

                public void InitializeComponent()
                {
                  this.StartupUri = new Uri("Window1.xaml", System.UriKind.Relative);
                }
                

                }
                }

                Predictably, you need to set the startup object to point to this class. The trick is that you physically aren't going to create any other instances of the app because you have detected the state of the mutex before you create an instance of the app. BTW - you really need to use the StartupUri, I suspect this is where you've been getting your problems.

                Deja View - the feeling that you've seen this post before.

                My blog | My articles

                J Offline
                J Offline
                Jammer 0
                wrote on last edited by
                #8

                Thanks for this Pete, i'm just looking at this code now, I take it its ok to add this into the existing App.xaml.cs file rather than creating a completely new class. Cheers,

                Jammer Going where everyone here has gone before! :) My Blog

                P 1 Reply Last reply
                0
                • J Jammer 0

                  Thanks for this Pete, i'm just looking at this code now, I take it its ok to add this into the existing App.xaml.cs file rather than creating a completely new class. Cheers,

                  Jammer Going where everyone here has gone before! :) My Blog

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #9

                  No. This should be a new class. WPF automatically generates an App entry, so you need to get round this by creating your own Application class - set the startup object to point to this in your project references.

                  Deja View - the feeling that you've seen this post before.

                  My blog | My articles

                  J 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    No. This should be a new class. WPF automatically generates an App entry, so you need to get round this by creating your own Application class - set the startup object to point to this in your project references.

                    Deja View - the feeling that you've seen this post before.

                    My blog | My articles

                    J Offline
                    J Offline
                    Jammer 0
                    wrote on last edited by
                    #10

                    Works like an absolute charm!!! Thanks for this Pete!!

                    Jammer Going where everyone here has gone before! :) My Blog

                    1 Reply Last reply
                    0
                    • J Jammer 0

                      Hi All, I need to make an application single instance, I've read and seen a few examples of doing this but none have really touched on WHERE to do this. In a WPF application with a normal App class inheriting from Application is this the best place to perform this check? I'm looking at the Adam Nathan book which has a code sample, but putting this into the App class doesn't seem to work (this wont compile!):

                      public partial class App : Application
                      {
                          bool mutexIsNew;
                          using (System.Threading.Mutex m = new System.Threading.Mutex(true, "uniquename", out mutexIsNew))
                          {
                              if(mutexIsNew)
                              // new instance
                              else
                              //shutdown
                          }
                      }
                      

                      Looking at this MS example http://msdn.microsoft.com/en-us/library/ms771662.aspx[^] rather than using a mutex (which seems the best way) they import a visual basic namespace into a csharp app!! Cheers,

                      Jammer Going where everyone here has gone before! :) My Blog

                      R Offline
                      R Offline
                      RNEELY
                      wrote on last edited by
                      #11

                      Yes the app class is the place to do the check. Try putting code inside the App:OnStartup() function. public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { bool mutexCreated; mutexRunOnce = new System.Threading.Mutex( true, "{someGUID}", out mutexCreated); if (!mutexCreated) { return; } base.OnStartup(e); } //... }

                      Sincerely, -Ron

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups