Force one copy of Application

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I saw that someone asked that question here before, and i tried one of the
answer that was gaven (Bob's tricks and tiips), The suggestion there was
using the ImoprtDll and tried to import the user32.dll, when i tried to
import it to the refernces, the Visual Studio crashed.

So how can i force one copy of application (i'll be happy and thankful to
get an example).

Thanks,
Gidi.
 
One simple way to do it is to list the running processes on the system and
see if another instance of the same name, running from the same location
exists, with a different process id exists.

Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName
(current.ProcessName);

//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.
Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return true;
}
}
}

Brendan
 
Brendan,

This is a horrible (and not guaranteed way) of allowing only one copy of
an application to run.

It can definitely impact performance (because of all the information
about the processes that have to be retreived).

The proper way to do this is to set up a mutex with a unique name, and
then try and access it. If you can not access it in your app (presumably
because another instance already holds it), then do not run it, and exit the
entry point of the application.

Check out my response to a similar question (watch for line wrap):

http://groups.google.com/group/micr...0637e010aec/07128f9ddd957ada#07128f9ddd957ada

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brendan Grant said:
One simple way to do it is to list the running processes on the system and
see if another instance of the same name, running from the same location
exists, with a different process id exists.

Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName
(current.ProcessName);

//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe
file.
if (Assembly.GetExecutingAssembly().Location.
Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return true;
}
}
}

Brendan


Gidi said:
Hi,

I saw that someone asked that question here before, and i tried one of
the
answer that was gaven (Bob's tricks and tiips), The suggestion there was
using the ImoprtDll and tried to import the user32.dll, when i tried to
import it to the refernces, the Visual Studio crashed.

So how can i force one copy of application (i'll be happy and thankful to
get an example).

Thanks,
Gidi.
 
Back
Top