• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 905
  • Last Modified:

C# Detect for start application twice time (advanced)

Hi experts!

  Im using C# 2008 , WinForm NET 2. I have application, which if is started with command parameters start different forms (and this form stay open all the time and can not be change).
I want to make sure that this application can not be started twice on 1 computer. This is simple, but not in this case. Because in fact application is count if is started twice with same parametyr. (possible option are only 3 and are static text like param1 or param2 or param3 How to do this ?

 In COM world I will ask and get for caption of application and will understand have or not have already started this application. But how I can do this via NET?

 I can save let say in registry this info, but if my application fail it will not save that is close and next time is possible to be unable to start J

 I need this  to keep from people error with started twice time my application.
1 Solution
Ensuring that only a single instance of a .NET application is running

    static void Main()                  // args are OK here, of course
        bool ok;
        m = new System.Threading.Mutex(true, "YourNameHere", out ok);
        if (! ok)
            MessageBox.Show("Another instance is already running.");
        Application.Run(new Form1());   // or whatever was there
        GC.KeepAlive(m);                // important!

Open in new window

Jaime OlivaresSoftware ArchitectCommented:
Rahul Goel ITILSenior Consultant - DeloitteCommented:
Hey this will help you.

you can use WindowsFormsApplicationBase class and check whether you appl has got different forms or not
in your Program.cs try this code.
in this example we avoid Login twice times.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices; 
    //static class Program
    //    /// <summary>
    //    /// The main entry point for the application.
    //    /// </summary>
    //    [MTAThread]
    //    static void Main()
    //    {
    //        try
    //        {
    //            Application.Run(new frmLogin());
    //        }
    //        catch (Exception ex)
    //        {
    //            MessageBox.Show(ex.ToString());
    //        }
    //    }
    static class Program
        // need this for single instance detection 
        private static extern int FindWindow(string lpClassName, string lpWindowName);
        private static extern bool SetForegroundWindow(int hwnd);
        const string appName = "Login";
        const int ALREADY_EXISTS = 183;
        static void Main()
            startTicks = Environment.TickCount;
            using (AppExecutionManager execMgr = new AppExecutionManager(appName))
                if (execMgr.IsFirstInstance)
                    Application.Run(new frmLogin());
                    int hWnd = FindWindow(null, appName);
        internal static int startTicks;

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now