C# Detect for start application twice time (advanced)

Posted on 2009-07-02
Last Modified: 2013-12-17
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.
Question by:dvplayltd
LVL 53

Accepted Solution

Dhaest earned 500 total points
ID: 24770285
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

LVL 55

Expert Comment

by:Jaime Olivares
ID: 24770350

Expert Comment

by:Rahul Goel
ID: 24770497
Hey this will help you.

you can use WindowsFormsApplicationBase class and check whether you appl has got different forms or not

Expert Comment

ID: 24770924
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


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What .NET website keeps me current? 9 55
What's the most efficient way to load up a couple of large table in C#? 5 36
Class object 2 26
Entity Framework 7 30
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
A simple description of email encryption using a secure portal service. This is one of the choices offered by The Email Laundry for email encryption. The other choices are pdf encryption which creates an encrypted pdf of your email and any attachmen…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now