Geeks With Blogs
Richard Taylor's Blog So Long, and Thanks for All the Code

I have C++ code to prevent more then one instance of an application from running at the same time and I need some managed code to do the same.

My C++ code just creates a Mutex and checks the Win32 error code to see if it indicates that the Mutex already exists. This has always worked well so I decided to just port the same code.

The code:

using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace Saic.Common.Util
{
  ///
  /// Determines if another instance of the application is already running.
  ///

  public class RunOnce
  {
    const int ERROR_ALREADY_EXISTS = 183;


    ///


    /// Used to determine if another instance of the application is already running.
    ///

    /// appName - The name of application. This must be unique for all applications.
    /// global - True to check all sessions on the machine and false to check the current session.
    /// returns - True if already running, false if not running
    static public bool Running(string appName, bool global)
    {
      bool ret = false;
      IntPtr mutexHandle
= IntPtr.Zero;


      try
      {
        string mutexName = (global ? @"Global\" : "") + appName + "RunOnce";
        mutexHandle
= CreateMutex(IntPtr.Zero, true, mutexName);
        if(mutexHandle != IntPtr.Zero)
        {
          if(Marshal.GetLastWin32Error() == ERROR_ALREADY_EXISTS)
          {
            ret
= true;
          }
        }
      }
      finally
      {
        if(mutexHandle != IntPtr.Zero)
        {
          ReleaseMutex(mutexHandle);
        }
      }


      return ret;
    }


    [ DllImport(
"kernel32", SetLastError=true, CharSet=CharSet.Auto) ]
    private static extern IntPtr CreateMutex(IntPtr lpSecurityAttributes, bool bInitialOwner, string lpName);

    [ DllImport("kernel32", SetLastError=true, CharSet=CharSet.Auto) ]
    private static extern bool ReleaseMutex(IntPtr hMutex);
  }
}

The Running() method takes two parameters. The application name should be unique for each application that uses the class. The global parameter determines if a global Mutex or a local Mutex is created. Calling the method with global set to false will only apply to the current session. Calling it with global set to true will apply to all sessions on the system. You can have different session if you are running Windows Server and have people logging in via Terminal Services. (HINT: If you want to log into the console session using remote desktop just pass the “/console” command line parameter to the remote desktop application.)

If the method returns true then another instance of the application is running.

 

Posted on Tuesday, March 14, 2006 7:00 PM General | Back to top


Comments on this post: How to prevent more then one instance of an application from running at the same time

# re: How to prevent more then one instance of an application from running at the same time
Requesting Gravatar...
You can use the Mutex class in the framework to do the same thing http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingmutexclasstopic.asp if it doesn't support everything you can also pinvoke the same WIN API calls.

Cheers,

Greg
Left by Greg Young on Mar 15, 2006 2:27 AM

Your comment:
 (will show your gravatar)


Copyright © Richard Taylor | Powered by: GeeksWithBlogs.net