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. C#
  4. How to know wheather a exe is running or not...

How to know wheather a exe is running or not...

Scheduled Pinned Locked Moved C#
csharptutorial
5 Posts 5 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.
  • R Offline
    R Offline
    Ravikumar Patra
    wrote on last edited by
    #1

    I have done a project in .net. I want that when a user tries to run the .exe, if it is already running then the same instance should be focused and no new instance should be creaed. what to do please let me know

    N L G E 4 Replies Last reply
    0
    • R Ravikumar Patra

      I have done a project in .net. I want that when a user tries to run the .exe, if it is already running then the same instance should be focused and no new instance should be creaed. what to do please let me know

      N Offline
      N Offline
      Navi15
      wrote on last edited by
      #2

      check out this code string aModuleName = system.Diagnostics.Process.GetCurrentProcess.MainModule.ModuleName; string aProcName = System.IO.Path.GetFileNameWithoutExtension(aModuleName); if (system.Diagnostics.Process.GetProcessesByName(aProcName).Length>1) { Application.Exit(); } Navin

      1 Reply Last reply
      0
      • R Ravikumar Patra

        I have done a project in .net. I want that when a user tries to run the .exe, if it is already running then the same instance should be focused and no new instance should be creaed. what to do please let me know

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

        Create a Mutex in the very first instance and if it already exists, then just quit your application. Some pseudocode:

        if(!Mutex.Exists("SomeName"))
        {
        Mutex.Create("SomeName");
        }
        else
        {
        // Quit
        }

        regards

        1 Reply Last reply
        0
        • R Ravikumar Patra

          I have done a project in .net. I want that when a user tries to run the .exe, if it is already running then the same instance should be focused and no new instance should be creaed. what to do please let me know

          G Offline
          G Offline
          gnjunge
          wrote on last edited by
          #4

          You can use a Mutex. Here is a piece of code that shows how to do it: In program.cs:

              private static System.Threading.Mutex \_mutex;
              private const string \_mutexName = "Company\_Product\_MUTEX";
          
             /// /// The main entry point for the application.
              /// 
              \[STAThread\]
              static void Main()
              {
          
                  //Allow only one instance for this app to be active
                  try
                  {
                      //try to open existing mutex
                      \_mutex = System.Threading.Mutex.OpenExisting(\_mutexName);
          
                      if (\_mutex != null)
                          return; //exit program;
                  }
                  catch
                  {   //There was no existing mutex so create mutex
                      try
                      {
                          \_mutex = new System.Threading.Mutex(true, \_mutexName);
                      }
                      catch { };
                  }
          
                  Application.ApplicationExit += new EventHandler(Application\_ApplicationExit);
          

          /// do rest of the code, like Application.Run() etc.

              }
          
              static void Application\_ApplicationExit(object sender, EventArgs e)
              {
                  //Release Mutex 
                  if (\_mutex != null)
                  {
                      \_mutex.ReleaseMutex();
                  }
              }
          

          That should do the trick.

          1 Reply Last reply
          0
          • R Ravikumar Patra

            I have done a project in .net. I want that when a user tries to run the .exe, if it is already running then the same instance should be focused and no new instance should be creaed. what to do please let me know

            E Offline
            E Offline
            Eric Dahlvang
            wrote on last edited by
            #5

            Single Instance[^]

            --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

            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