|
-
May 3rd, 2009, 12:07 PM
#1
[RESOLVED] Allow only one instance of the program to run ?
how can i ensure that only one instance of my program is running at anytime and don't allow second instance to run if the first instance is still running. I've read somewhere that i have to use a Mutex variable; but i don't know how to use it.
-
May 3rd, 2009, 01:02 PM
#2
Re: Allow only one instance of the program to run ?
-
May 4th, 2009, 01:59 AM
#3
Re: Allow only one instance of the program to run ?
yes, one way is Mutex because it is cross process accesible Object.
another way is looking at list of running processes using ProcessInfo.
Please rate my post if it was helpful for you.  Java, C#, C++, PHP, ASP.NET
SQL Server, MySQL
DirectX
MATH Touraj Ebrahimi
[toraj_e] [at] [yahoo] [dot] [com]
-
May 4th, 2009, 03:19 AM
#4
Re: Allow only one instance of the program to run ?
I'd go with toraj's suggestion, something like this:
Code:
using System;
using System.Diagnostics;
namespace MyApplication
{
class Program
{
static void Main()
{
Process[] procs = Process.GetProcessesByName("MyApplication");
if (procs.Length > 1)
return;
else
Console.WriteLine("Hello World!");
}
}
}
It's not a bug, it's a feature!
-
May 4th, 2009, 10:39 AM
#5
Re: Allow only one instance of the program to run ?
i've tried foamy's way of getting the number of processes by name and seeing if its larger than 1 then close the new process and it worked. But i have sort of a small question; when i run the application from visual studio the process name is "processName.vshost.exe" but when i run it from the its exe file its name is normal i.e. "processName.exe"
anyway thank you all for the help;
Best Regards;
bluebarca
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|