Checking Previous Instance (Single Instance) Of An C# Windows Application, Also Setting MDI Child Form To A MDI Parent

Introduction

This article will describe two things.

  1. How to check previous instances of a C# Windows application.
  2. How to check that 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.

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

Check the Previous Instance of a Windows Application

using System.Diagnostics;
using System.Windows.Forms;
private static bool GetPrevInstance()
{
    // Get the name of the 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[] allProcessesWithThisName = Process.GetProcessesByName(currPrsName);
    // If more than one process is running, return true.
    // This means a previous instance of the application is already running
    if (allProcessesWithThisName.Length > 1)
    {
        MessageBox.Show("Already Running");
        return true; // Yes, previous instance exists
    }
    else
    {
        return false; // No previous instance running
    }
}

Note. Instead of the above method of checking by process name, we can also use a mutex, but this method is easy as compared to the mutex, which requires a knowledge of threading.

This method will work fine unless and until you change the process name.

Check Whether an MDI Child is Set to an MDI parent

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

Suppose I have a Windows form named as from module, and I want to set it as an MDI child to MDI Form. I am calling the function ActivateThisChill() with the name of the form as a parameter to this function.

using System.Diagnostics;
using System.Windows.Forms;
private static bool GetPrevInstance()
{
    // Get the name of the 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[] allProcessesWithThisName = Process.GetProcessesByName(currPrsName);
    // If more than one process is running, return true.
    // This means a previous instance of the application is already running
    if (allProcessesWithThisName.Length > 1)
    {
        MessageBox.Show("Already Running");
        return true; // Yes, the previous instance exists
    }
    else
    {
        return false; // No previous instance running
    }
}

Note. The other way around is to use 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 your actions accordingly.


Similar Articles