473,406 Members | 2,273 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,406 software developers and data experts.

Running only 1 application instance in a machine

Avoid running several simultaneous instances of the same application on
a single machine. ( Checking if application is already running)

(Book Excerpt Practical C#2)

Thanks to the static methods named GetCurrentProcess() (which returns
the current
process) and GetProcesses() (which returns all the processes on the
machine) of the System.
Diagnostics.Process class, this problem finds an elegant and easy to
implement solution using
the following program:
using System.Diagnostics;

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (TestIfAlreadyRunning())
{
MessageBox.Show("Application is already running");
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}

static bool TestIfAlreadyRunning()
{
Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
if (process.Id != currentProcess.Id)
if (process.ProcessName ==
currentProcess.ProcessName)
return true;
}
return false;
}
}
The GetProcesses() method can also return all the processes on a remote
machine by indicating the name of the machine as a parameter to the
method.

Mar 2 '06 #1
2 1646
And what if user decided to rename app to call one more instance? :)
Why not to use Global mutext.

--
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche

Mar 2 '06 #2
Him

take a look at
http://www.yoda.arachsys.com/csharp/...ation.instance
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<an******@hotmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Avoid running several simultaneous instances of the same application on
a single machine. ( Checking if application is already running)

(Book Excerpt Practical C#2)

Thanks to the static methods named GetCurrentProcess() (which returns
the current
process) and GetProcesses() (which returns all the processes on the
machine) of the System.
Diagnostics.Process class, this problem finds an elegant and easy to
implement solution using
the following program:
using System.Diagnostics;

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (TestIfAlreadyRunning())
{
MessageBox.Show("Application is already running");
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}

static bool TestIfAlreadyRunning()
{
Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
if (process.Id != currentProcess.Id)
if (process.ProcessName ==
currentProcess.ProcessName)
return true;
}
return false;
}
}
The GetProcesses() method can also return all the processes on a remote
machine by indicating the name of the machine as a parameter to the
method.

Mar 2 '06 #3

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

Similar topics

9
by: Marina Anufreichik | last post by:
Hi, After deploymnet web application on web server I can access page on local machine and login fine but when I'm trying to access web site from remote machine I can see login page, but when I'm...
1
by: Mandhare Prashant | last post by:
Hello, I have Mapoint software installed on my machine. Now, in VC++, I can use this mapoint application in my program as below- _Application mapapp; _Map map;
6
by: Moses M | last post by:
I posted this a short while ago , but I don't think I explained the problem clearly. Task Manager lists processes running on a local system, including a "user name" associated with each process...
6
by: Alex G. | last post by:
some one knows how to check if my app. (Example ap1.exe) is running (because i don't want than it run twice at the same time in the same machine) somebody knows like verifying if my application...
3
by: Oenone | last post by:
In VB6, it's possible to create a project with a class library within it. The project can then be run with F5, and it sits and waits for an external process to create an instance of one of its...
3
by: Michel | last post by:
Hi, I wrote an app in .Net and I whant only 1 instance of this app open for the user; the user open my app, do some works and try to open another instance of my app, I whant to show a message to...
3
by: Olie | last post by:
I have a number of applications that need to access the same object at different times and any one of these programs may be running at any one time. I do not have a specific application that could...
14
by: Alex K. | last post by:
Hi all I'd like to avoid starting the same c# application twice on the same computer. How do I check if it is already running? Thank you
0
by: mogenses | last post by:
This is going to be a good one but may be easy for you experts. I have recently joined a tech support dept supporting an application using ACCESS 2.0 DATABASES and I believe written in Fox Pro. ...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.