473,387 Members | 1,318 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,387 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 1429
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.