Tuesday, May 15, 2012

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

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.

No comments:

Post a Comment