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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Single instance of application

Single instance of application

Scheduled Pinned Locked Moved C#
6 Posts 6 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.
  • P Offline
    P Offline
    Parshant Verma
    wrote on last edited by
    #1

    Hi I devloped a application.Every time clicking on .exe a instance is opened of that application.How this is possible that single instance run of this application

    A S E 3 Replies Last reply
    0
    • P Parshant Verma

      Hi I devloped a application.Every time clicking on .exe a instance is opened of that application.How this is possible that single instance run of this application

      A Offline
      A Offline
      Andrei Ungureanu
      wrote on last edited by
      #2

      Hy, Use the Process.GetProcessesByName(processname) class to see if another process with the same name is running. Hope it helps.

      Do your best to be the best

      L 1 Reply Last reply
      0
      • P Parshant Verma

        Hi I devloped a application.Every time clicking on .exe a instance is opened of that application.How this is possible that single instance run of this application

        S Offline
        S Offline
        Stefan Troschuetz
        wrote on last edited by
        #3

        There are some articles here on CP covering this topic. Simply search the artciles for "single instance application".


        "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

        www.troschuetz.de

        1 Reply Last reply
        0
        • A Andrei Ungureanu

          Hy, Use the Process.GetProcessesByName(processname) class to see if another process with the same name is running. Hope it helps.

          Do your best to be the best

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          This is not the best way to do it, it's quite error prone. The correct way is to use a Mutex and check if it exists.

          1 Reply Last reply
          0
          • P Parshant Verma

            Hi I devloped a application.Every time clicking on .exe a instance is opened of that application.How this is possible that single instance run of this application

            E Offline
            E Offline
            engsrini
            wrote on last edited by
            #5

            This is the code your looking for.

            [STAThread]
            static void Main()
            {
            //Get the running instance.
            Process instance = RunningInstance();
            if (instance == null)
            {
            //There isn't another instance, show our form.

                            Application.Run(new AteGui(splashScrn));            
                        
                    }
                    else
                    {
                        //There is another instance of this process.
                        HandleRunningInstance(instance);
                    }
                }
            

            Helper Functions and DLL imports
            /// /// Property to set the visible status of the Tutorial Button
            ///
            public static Process RunningInstance()
            {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);

                    //Loop through the running processes in with the same name
                    foreach (Process process in processes)
                    {
                        //Ignore the current process
                        if (process.Id != current.Id)
                        {
                            //Make sure that the process is running from the exe file.
                            if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\\\") ==
                            current.MainModule.FileName)
                            {
                                //Return the other process instance.
                                return process;
                            }
                        }
                    }
            
                    //No other instance was found, return null.
                    return null;
                }
            
            
                public static void HandleRunningInstance(Process instance)
                {
                    //Make sure the window is not minimized or maximized
                    ShowWindowAsync(instance.MainWindowHandle, WS\_SHOWNORMAL);
            
                    //Set the real intance to foreground window
                    SetForegroundWindow(instance.MainWindowHandle);
                }
            
                 \[DllImport("User32.dll")\] 
            
                private static extern bool ShowWindowAsync(
                 IntPtr hWnd, int cmdShow);
                 \[DllImport("User32.dll")\] private static extern bool
                 SetForegroundWindow(IntPtr hWnd);
                 private const int WS\_SHOWNORMAL = 1;
            
            M 1 Reply Last reply
            0
            • E engsrini

              This is the code your looking for.

              [STAThread]
              static void Main()
              {
              //Get the running instance.
              Process instance = RunningInstance();
              if (instance == null)
              {
              //There isn't another instance, show our form.

                              Application.Run(new AteGui(splashScrn));            
                          
                      }
                      else
                      {
                          //There is another instance of this process.
                          HandleRunningInstance(instance);
                      }
                  }
              

              Helper Functions and DLL imports
              /// /// Property to set the visible status of the Tutorial Button
              ///
              public static Process RunningInstance()
              {
              Process current = Process.GetCurrentProcess();
              Process[] processes = Process.GetProcessesByName(current.ProcessName);

                      //Loop through the running processes in with the same name
                      foreach (Process process in processes)
                      {
                          //Ignore the current process
                          if (process.Id != current.Id)
                          {
                              //Make sure that the process is running from the exe file.
                              if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\\\") ==
                              current.MainModule.FileName)
                              {
                                  //Return the other process instance.
                                  return process;
                              }
                          }
                      }
              
                      //No other instance was found, return null.
                      return null;
                  }
              
              
                  public static void HandleRunningInstance(Process instance)
                  {
                      //Make sure the window is not minimized or maximized
                      ShowWindowAsync(instance.MainWindowHandle, WS\_SHOWNORMAL);
              
                      //Set the real intance to foreground window
                      SetForegroundWindow(instance.MainWindowHandle);
                  }
              
                   \[DllImport("User32.dll")\] 
              
                  private static extern bool ShowWindowAsync(
                   IntPtr hWnd, int cmdShow);
                   \[DllImport("User32.dll")\] private static extern bool
                   SetForegroundWindow(IntPtr hWnd);
                   private const int WS\_SHOWNORMAL = 1;
              
              M Offline
              M Offline
              mav northwind
              wrote on last edited by
              #6

              As has been stated before, iterating through a process list is not a good idea because of several reasons (with being slow the least one). Mutexes are the way to go!

              Regards, mav -- Black holes are the places where God divided by 0...

              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