This article will describes two things:
- How to check for an existing instance of a C# Windows application.
- 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
private static bool getPrevInstance()
string currPrsName = Process.GetCurrentProcess().ProcessName;
if (allProcessWithThisName.Length > 1)
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
Mutex m = new Mutex(true, "ApplicationName", out appNewInstance);
MessageBox.Show("One Instance Of This App Allowed.");
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;
notifyIcon1.Visible = true;
notifyIcon1.BalloonTipTitle = " Project Tracker Suite (PTS)";
notifyIcon1.BalloonTipText = " PTS: Modules";
private Boolean ActivateThisChild(String formName)
Boolean formSetToMdi = false;
for (i = 0; i < this.MdiChildren.Length; i++)
if (this.MdiChildren[i].Name == formName)
formSetToMdi = true;
if (i == 0 || formSetToMdi == false)
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:
- Using Crystal Reports with Oracle, and Parameterized Query (passing SQL query parameters to Crystal Reports)
- Attaching a digital certificate (public key) to an HTTPS SSL request