473,288 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

Force one copy of Application

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.
Nov 17 '05 #1
2 1427
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" wrote:
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.

Nov 17 '05 #2
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/micro...128f9ddd957ada

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brendan Grant" <gr****@NOSPAMdahat.com> wrote in message
news:4A**********************************@microsof t.com...
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" wrote:
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.

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Navin | last post by:
hi, guys i am using the following code to force a file download dialog in asp Response.ContentType = "application/vnd.ms-excel" response.AddHeader "content-disposition","attachment; filename="...
2
by: Raquel | last post by:
FORCE APPLICATION command is valid only at the 'instance' level. Why is this so? An instance may contain many databases. So, what command do I give if I want to force applications from a particular...
2
by: Raed Sawalha | last post by:
how can I view only one exec copy of my application? double click app icon-> open-> again double click app icon-> i need to check it already running not to open it again. regards
2
by: Oenone | last post by:
I have an application which loads plugin DLLs from various directories on the local disk. To avoid problems with the DLLs being locked by IIS, I have modified my code so that it copies the DLLs to...
0
by: comp.lang.php | last post by:
I have a form that when you click the "Generate Report" submit button, it will force download a CSV file, required for this project. On the very same page you also have a "Search" submit button,...
2
by: comp.lang.php | last post by:
class ReportGenerator { function ReportGenerator() {} /** * Generate the HTTP headers necessary for this file type. Can be called statically *
3
by: MarkusJNZ | last post by:
Hi, I have two different applications running on 2003 both in two different vitrual directories. One application runs on the .NET framework 1.1 whilst the other runs on 2.0. If I change the...
0
by: savajx1 | last post by:
I am loading signed assemblies from a network share. The assemblies are NOT INSTALLED in the GAC as they are part of an enterprise distributed application implemented as a windows service that...
3
by: Dean Slindee | last post by:
Using VS2005, I am deploying a WinForm application with ClickOnce. The project contains a ReportViewer2005 control, so there is a prerequisite for the ReportViewer2005.dll. The ReportViewer.dll...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.