Click here to Skip to main content
15,886,919 members
Articles / Programming Languages / C#
Tip/Trick

Run only one instance from you program

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
23 Sep 2010CPOL 25.6K   11  
C#
private void XXXXX_Load(object sender, EventArgs e)
{
    string m_UniqueIdentifier;
    string assemblyName =
System.Reflection.Assembly.GetExecutingAssembly().GetName(false).CodeBase;
    m_UniqueIdentifier = assemblyName.Replace("\\", "_");
    m_Mutex = new Mutex(false, m_UniqueIdentifier);
    if (m_Mutex.WaitOne(1, true))
    {
    }
    else
    {
        //Not the first instance!!!
        m_Mutex.Close();
        m_Mutex = null;
        MessageBox.Show("Start only one Instance", "Allways runnning");
        this.Close();
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --