CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2005
    Posts
    2

    Question Prevent runing same executable

    Hi
    I created a C# sharp application and it has no threads. I want prevent someone executing the same application if it is currently executing.

    Thanks
    Reginold

  2. #2
    Join Date
    Jun 2005
    Location
    Georgia, USA
    Posts
    19

    Re: Prevent runing same executable

    Use this code in your Main() function:

    static void Main()
    {
    string procName = Application.ProductName;
    System.Diagnostics.Process[] myProcesses
    = System.Diagnostics.Process.GetProcessesByName(procName);

    if (myProcesses.Length > 1)
    {
    MessageBox.Show("Application Already Open");
    return;
    }

    // Now start the application
    Application.Run(new Form1());
    }

  3. #3
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Prevent runing same executable

    Quote Originally Posted by rchrishantha
    Hi
    I created a C# sharp application and it has no threads. I want prevent someone executing the same application if it is currently executing.

    Thanks
    Reginold
    if your app has no threads, thats pretty much going to guarantee that no one will be able to use it, because any running app is going to be on *some* thread.
    Last edited by MadHatter; June 20th, 2005 at 04:07 PM.

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

    Re: Prevent runing same executable

    Here is another idea:
    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);
         }
    }
    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());
    }

  5. #5
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Prevent runing same executable

    or this one,
    Code:
    static void Main()
    Int32 isProcessRunning;
    isProcessRunning = System.Diagnostics.Process.GetProcessesByName(
      System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length;
    if(isProcessRunning != 1)
    {
      MessageBox.Show("Already an Instance running");
    }
    else
    {
      Application.Run(new Form1());
    }

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Prevent runing same executable

    Quote Originally Posted by MadHatter
    if your app has no threads, thats pretty much going to guarantee that no one will be able to use it, because any running app is going to be on *some* thread.
    rofl..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

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