Introduction
This article will describe two things:
- How to check previous instance of a C# windows application.
- How to check that whether a MDI child is set to a mdiparent, and if it is already loaded then just activate it instead of creating a new instance and setting it as MDI child.
This code is written in C#. This article will especially help those who were VB 6.0 programmer and recently shifted to .NET.
Check the Previous 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 mutex,but thismethod is easy as compared to the mutex which requires a knowledge of threading.
This method will work fine unless and untill you change the process name.
Check Whether a MDI Child is Set to a mdiparent
The below code will check that whether a MDI child is set to a mdi parent and if it is already loaded and a 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 frmModule and I want to set it as a mdi child to MDI Form. I am calling function ActivateThisChill() with the name of the form as a parameter to this function.
{
if (ActivateThisChild("frmModule") == false)
{
frmModule newMDIChild = new frmModule();
newMDIChild.Text = "Module";
// The StartPosition property is essential
// for the location property to work
newMDIChild.StartPosition = FormStartPosition.Manual;
newMDIChild.Location = new Point(0,0);
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 static property in your each form and check in your MDI parent that whether the static property of that particular form is assigned a value or not and then take your actions accordingly.
No comments:
Post a Comment