Click here to Skip to main content
Click here to Skip to main content

Checking for an existing instance of a Windows application, and setting the MDI parent form of an MDI child

By , 8 Sep 2008
 

Introduction

This article will describes two things:

  1. How to check for an existing instance of a C# Windows application.
  2. How to check whether a MDI child is set to an MDI parent, and if it is already loaded, then just activate it instead of creating a new instance and setting it as an MDI child.

The example code is written in C#. This article will especially help those who were VB 6.0 programmers and recently shifted to .NET.

Check for an existing instance of a Windows application

using System.Diagnostics;

private static bool getPrevInstance()
{
    //get the name of current process, i,e the process 
    //name of this current application

    string currPrsName = Process.GetCurrentProcess().ProcessName;

    //Get the name of all processes having the 
    //same name as this process name 
    Process[] allProcessWithThisName 
                 = Process.GetProcessesByName(currPrsName);

    //if more than one process is running return true.
    //which means already previous instance of the application 
    //is running
    if (allProcessWithThisName.Length > 1)
    {
        MessageBox.Show("Already Running");
        return true; // Yes Previous Instance Exist
    }
    else
    {
        return false; //No Prev Instance Running
    }
}

Note: Instead of the above method of checking by process name, we can also use a mutex, but this method is easy compared to the mutex one, which requires a knowledge of threading. This method will work fine unless and until you change the process name for some specific reasons.

Alternate way using Mutex

using System.Threading;
bool appNewInstance;

Mutex m = new Mutex(true, "ApplicationName", out appNewInstance);

if (!appNewInstance)
{
    // Already Once Instance Running
    MessageBox.Show("One Instance Of This App Allowed.");
    return;
}
GC.KeepAlive(m);

Check whether an MDI child is set to an MDI parent

The below code will check whether an MDI child is set to an MDI parent, and if it is already loaded, then just activate it instead of creating a new instance and setting it as an MDI child.

Suppose I have a Windows form named frmModule and I want to set it as an MDI child of an MDI form. I can call the function ActivateThisChill() with the name of the form as a parameter, to accomplish this:

if (ActivateThisChild("frmModule") == false)
{
    frmModule newMDIChild = new frmModule();
    newMDIChild.Text = "Module";
    newMDIChild.MdiParent = this;
    newMDIChild.Show();
}
notifyIcon1.Visible = true;
notifyIcon1.BalloonTipTitle = " Project Tracker Suite (PTS)";
notifyIcon1.BalloonTipText =  " PTS: Modules";
notifyIcon1.ShowBalloonTip(2000);


//This function will return true or false. 
//false means this form was not previously set 
//as mdi child hence needs to creat a instance
//and set it as a mdi child.


private Boolean ActivateThisChild(String formName)
{
    int i;
    Boolean formSetToMdi = false;
    for (i = 0; i < this.MdiChildren.Length; i++)
     // loop for all the mdi children
    {
        if (this.MdiChildren[i].Name == formName)
          // find the Mdi child with the same name as your form
        {
            // if found just activate it
            this.MdiChildren[i].Activate();
            formSetToMdi = true;
        }
    }

    if (i == 0 || formSetToMdi == false)
        // if the given form not found as mdi child return false.
        return false;

    else
        return true;


}

Note: The other way around is to use a static property in each form and check in your MDI parent whether the static property of that particular form is assigned a value or not, and then take actions accordingly.

Points of interest

My other articles of interest are:

  1. Using Crystal Reports with Oracle, and Parameterized Query (passing SQL query parameters to Crystal Reports)
  2. Attaching a digital certificate (public key) to an HTTPS SSL request

License

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

About the Author

Rehan Ahmad Abbasi
Software Developer (Senior) Al-Jazirah Corporation Riyadh KSA
United Arab Emirates United Arab Emirates
I am a Software Engineer having 4 + years of experience in various skill sets.
Proficiency in Asp.net, C#.net, Vb.net, Ado.net, VB 6.0, J2ME, Ajax, Xslt, Xml, Smart Device (Pocket PC 2003), and Oracle.
 
Extensive experience with analyzing, designing, development, and maintenance of Internet, Intranet, Client Server and Object Oriented applications built on .NET Framework (windows and web app.) and VB 6.0.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThanks PinmemberMember 854192023-May-13 16:13 
GeneralChecking for an instance of an MDI Child Pinmemberdevrw21-Jan-09 6:01 
GeneralInteresting topic! Pinmemberfinal_zero4-Sep-08 7:59 
GeneralPlease Vote For this article [modified] PinmemberRehan Ahmad Abbasi24-Aug-08 2:57 
GeneralThis got me thinking PinmemberIlíon21-Aug-08 5:17 
GeneralSetting the process name PinmemberRichardDeZwart21-Aug-08 2:52 
Generaljust 1 flaw PinmemberXaroth20-Aug-08 9:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 8 Sep 2008
Article Copyright 2008 by Rehan Ahmad Abbasi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid