Results 1 to 15 of 15

Thread: Creating a Single Instance of your Application

Hybrid View

  1. #1
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Allowing only once instance of your application

    This will only allow one instance of your application, i use this code for a couple of my programs/services...

    Make sure you maker a reference to the System.Management class

    C# Code:
    1. static void Main()
    2.         {
    3.             Application.EnableVisualStyles();
    4.             Application.SetCompatibleTextRenderingDefault(false);
    5.            
    6.             int count = CheckForRunningInstances();
    7.  
    8.             if (count <= 1)
    9.             {
    10.                 Application.Run(new Form1());
    11.             }
    12.             else
    13.             {
    14.                 System.Windows.Forms.MessageBox.Show("Another instance of this program is already running.  Two instances cannot run at the same time", "Already running", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    15.                 Application.Exit();
    16.             }
    17.  
    18.         }
    19.         private static int CheckForRunningInstances()
    20.         {
    21.  
    22.             string[] parts = System.Reflection.Assembly.GetExecutingAssembly().Location.Split("\\".ToCharArray());
    23.             string appName = parts[parts.Length - 1];
    24.  
    25.             string query = "select name from CIM_Process where name = '" + appName + "'";
    26.  
    27.             System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(query);
    28.  
    29.             int hInstCount = 0;
    30.  
    31.             foreach (System.Management.ManagementObject item in searcher.Get())
    32.             {
    33.                 hInstCount++;
    34.                 if (hInstCount > 1) break;
    35.             }
    36.             return hInstCount;
    37.         }
    Last edited by Paul M; Sep 11th, 2007 at 01:40 AM.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Creating a Single Instance of your Application

    kasracer,

    Have you ever looked at any code that does this, but also can send any command line arguments from the second instance that is terminated, to the first instance that was already running?

    It would exist in the "app already open, user double clicks file in windows that is associated with said app, about to be terminated instance needs to tell already open instance to handle the file" category.

    I will go about figuring it out, as I have a need to do this, however I figured I would ask you first since you have this codebank entry.

    In VB 2005, setting the "single instance application" checkbox accomplishes this well, however there is a nasty bug that has to do with that feature and firewall software that I need to try to eliminate. They use .NET remoting to accomplish this in VB 2005, which would not be an option to avoid the bug.

    In 2008 you can used named pipe classes to do this, however the project it is for is not moving to a new framework anytime soon.

    I have also seen some heavy API-centric methods of doing this that basically date back to VB6 but were ported to .NET before the whole single instance checkbox feature existed, however I was looking for a more managed code way first before I resort to direct calls to the Windows API.

  3. #3

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Creating a Single Instance of your Application

    Quote Originally Posted by kleinma
    Have you ever looked at any code that does this, but also can send any command line arguments from the second instance that is terminated, to the first instance that was already running?

    It would exist in the "app already open, user double clicks file in windows that is associated with said app, about to be terminated instance needs to tell already open instance to handle the file" category.

    I will go about figuring it out, as I have a need to do this, however I figured I would ask you first since you have this codebank entry.
    I have not seen any code that can do that. I've looked into it but haven't found an easy way to do so. If you come up with a way or find a way that allows sending of commands to the already opened application without the use of APIs then please share
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

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