How can I have one and just one copy of my application running in the same time???

How can I have one and just one copy of my application running in the same time




Answer this question

How can I have one and just one copy of my application running in the same time???

  • mcrisf

    I did somethign similar a while ago; doing something like this is so popular that I blogged about it here: http://dotnet.org.za/ernst/archive/2004/07/20/2887.aspx

    What makes it neat is that, if an existing instance of your application is already running, it will bring it to the foreground and maximize it. With the inspiringly named "helper" class (linked to from this blog-post) you're able to write:

    public static void Main()
    {
    bool firstInstance;
    Mutex mut = new Mutex(true, strSystemMutex, firstInstance);
    if (firstInstance) {
    try {
    Application.EnableVisualStyles();
    Application.Run(new frmLogin());
    }
    catch (Exception ex) {
    ExceptionManager.Publish(ex);
    } finally {
    mut.ReleaseMutex();
    }
    } else {
    try {
    Process[] processes = Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName);
    Process proc;
    foreach (Process proc in processes) {
    if (!proc.Id == Process.GetCurrentProcess.Id) {
    WindowsHelper.ActivateWindowByHandle(UInt32.Parse(proc.MainWindowHandle.ToString()));
    System.Environment.Exit(0);
    }
    }
    } catch (Exception ex) {
    ExceptionManager.Publish(ex);
    }
    }
    }


    Hope this helps!!!

    -Ernst



  • Derek Dowle

    /// Summary description for ProcessUtils.

    public static class ProcessUtils

    {

    private static Mutex mutex = null;

    /// Determine if the current process is already running

    public static bool ThisProcessIsAlreadyRunning()

    {

    // Only want to call this method once, at startup.

    Debug.Assert(mutex == null);

    // createdNew needs to be false in .Net 2.0, otherwise, if another instance of

    // this program is running, the Mutex constructor will block, and then throw

    // an exception if the other instance is shut down.

    bool createdNew = false;

    mutex = new Mutex(false, Application.ProductName, out createdNew);

    Debug.Assert(mutex != null);

    return !createdNew;

    }

    [DllImport("user32.dll", SetLastError = true)]

    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll")]

    [return: MarshalAs(UnmanagedType.Bool)]

    static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll")]

    static extern bool IsIconic(IntPtr hWnd);

    [DllImport("user32.dll")]

    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    const int SW_RESTORE = 9;

    [DllImport("user32.dll")]

    static extern IntPtr GetLastActivePopup(IntPtr hWnd);

    [DllImport("user32.dll")]

    static extern bool IsWindowEnabled(IntPtr hWnd);

    /// Set focus to the previous instance of the specified program.

    public static void SetFocusToPreviousInstance(string windowCaption)

    {

    // Look for previous instance of this program.

    IntPtr hWnd = FindWindow(null, windowCaption);

    // If a previous instance of this program was found...

    if (hWnd != null)

    {

    // Is it displaying a popup window

    IntPtr hPopupWnd = GetLastActivePopup(hWnd);

    // If so, set focus to the popup window. Otherwise set focus

    // to the program's main window.

    if (hPopupWnd != null && IsWindowEnabled(hPopupWnd))

    {

    hWnd = hPopupWnd;

    }

    SetForegroundWindow(hWnd);

    // If program is minimized, restore it.

    if (IsIconic(hWnd))

    {

    ShowWindow(hWnd, SW_RESTORE);

    }

    }

    }

    }

    ////////////////////////////////////////////////////

    static class Program

    {

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main()

    {

    if (ProcessUtils.ThisProcessIsAlreadyRunning())

    {

    // "Form1" is the caption (Text property) of the main form.

    ProcessUtils.SetFocusToPreviousInstance("Name of your windows form");

    }

    else

    {

    Application.EnableVisualStyles();

    Application.SetCompatibleTextRenderingDefault(false);

    Application.Run(new Form1());

    }

    }

    }

    ////

    So, you have a single class that you just calls these methods. I got this code from somewhere, I can't for the life of me recall who to credit for the class.



  • How can I have one and just one copy of my application running in the same time???