CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2005
    Location
    Australia
    Posts
    101

    Prevent an Application starting more than once

    Is there a way to prevent a user from starting the same application more than once?

  2. #2
    Join Date
    May 2004
    Location
    Greenville, NC
    Posts
    193

    Re: Prevent an Application starting more than once

    I found this code.

    Code:
    Process ThisProcess = Process.GetCurrentProcess();
    Process[] AllProcesses = Process.GetProcessesByName(ThisProcess.ProcessName);
    if (AllProcesses.Length > 1)
    {
        MessageBox.Show(ThisProcess.ProcessName + " is already running",
            ThisProcess.ProcessName,
            MessageBoxButtons.OK,
            MessageBoxIcon.Error);
    }
    else
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1()); 
    }
    You can put this in your Main method and it should work.
    Jason Isom
    .NET 2.0 / VS2005 Developer

  3. #3
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    Re: Prevent an Application starting more than once

    On startup you can use the FindWindow API call to check if any windows with same name are active.

  4. #4
    Join Date
    Sep 2005
    Location
    Australia
    Posts
    101

    Re: Prevent an Application starting more than once

    Jason, Do I place this code in the Load Event of the form?

    Sahir, is there some sample code for the FindWindow way?

  5. #5
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Prevent an Application starting more than once

    put this code in the main function:
    Code:
    using System.Threading
    using System.Runtime.InteropServices;
    public class Form1 : Form
    {
         [STAThread]
         static void Main() 
         {
              bool createdNew;
              Mutex m = new Mutex(true, "YourAppName", out createdNew);
              if (! createdNew)
              {
                   // app is already running...
                   MessageBox.Show("Only one instance of this application is allowed at a time.");
                   return;
              }
              Application.Run(new Form1());
              // keep the mutex reference alive until the normal termination of the program
              GC.KeepAlive(m);
         }
    }
    Make sure not to forget putting your application name instead of "YourAppName" above (in the mutex decleration).

    The next section will close the PREVIOUS instance of the application:
    Code:
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    
     
    
    static void Main() 
    {
          // get the name of the current process
          Process currentProcess = Process.GetCurrentProcess();
          string currProcessName = currentProcess.ProcessName;
    
          // get all instances of the this application
          Process[] prevAppInstance = Process.GetProcessesByName(currProcessName);
       
          // kill all other instances of this application
          for(int i = 0 ; i<prevAppInstance.GetLength(0) ; i++)
          {
                  if(prevAppInstance[i].Id != currentProcess.Id)
                  {
                          prevAppInstance[i].Kill();
                   }
          }
    
          // run the aplication
          Application.Run(new Form1());
    }
    Last edited by jhammer; September 20th, 2005 at 05:51 AM.

  6. #6
    Join Date
    Sep 2005
    Location
    Australia
    Posts
    101

    Re: Prevent an Application starting more than once

    jhammer,

    Thanks for that. Which one do you prefer to use? The first one or the second one?

  7. #7
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    Re: Prevent an Application starting more than once

    Quote Originally Posted by CSD
    Sahir, is there some sample code for the FindWindow way?
    Yes you probably can find plenty of examples in MSDN and here on Codeguru as well, but I think Jason's suggestion is a better one. Do let us know if it works.

  8. #8
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Prevent an Application starting more than once

    Quote Originally Posted by CSD
    Which one do you prefer to use? The first one or the second one?
    Well, that depends on your needs. If you want to run an application and than prevent running it again, then the first one will do.
    If you want to kill the current instance running, and run it all over again, then go for the second one.

  9. #9
    Join Date
    Sep 2005
    Location
    Australia
    Posts
    101

    Re: Prevent an Application starting more than once

    jhammer,

    I have implemeted your first option and it works very well. It is exactly what I needed.

    Cheers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured