Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if Application is already running

Status
Not open for further replies.

cpope

Programmer
Jul 7, 2000
58
US
I am trying to make sure only one instance of my application is running at one time. This code does not work. The Length is always 0. Does anyone know how to accomplish this task?

if(System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length > 1)
{
MessageBox.Show("Message Board Is Already Running","Message Board",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
this.Close();
}
 
I see it runs OK when I put them in the main entry point of the application and instead of "this.Close" I use "return".
 
I am still having trouble, the app still thinks there are 0 processes of the name running and allows me to open multiple instances of the application... here is the new code:

[STAThread]
static void Main()
{
Int32 runningProcesses = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length;
MessageBox.Show(Process.GetCurrentProcess().ProcessName +" "+ runningProcesses);

if(System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length > 0)
{
MessageBox.Show("Message Board Is Already Running","Message Board",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
return;
}
else
{
Application.Run(new ClientWindow());
}
}
 
*** FOUND A SOLUTION *****
Here is code that I found worked for my application:


static void Main()
{
Mutex currapp = new Mutex(false,&quot;<MessageBoardMutex>&quot;);
if(!currapp.WaitOne(0,false))
{
MessageBox.Show(&quot;Message Board Is Already Running&quot;,&quot;Message Board&quot;,MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
return;
}
else
Application.Run(new ClientWindow());
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top