471,317 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Number of instances of a program.

Hi,

What is the cleverest way of preventing the user of a program to start more
than instance of the program.

regards Jesper.
Feb 21 '06 #1
3 1081
Hi Jesper,

include the following code in the static void Main(string[] args) routine:

// start application
try {

// get the mutex

MyMutex = new System.Threading.Mutex(true, "Test", out Ownership);

// check, if a single instance of the programm runs

if(Ownership) {

Application.Run(new MainForm());

}

else {

System.Environment.Exit(-1);

}

// clean up the mutex

if(MyMutex != null) MyMutex.Close();

}

catch (Exception except) {

// clean up the mutex

if(MyMutex != null) MyMutex.Close();

System.Environment.Exit(-1);

}

Bye

Frank

"Jesper" <Je****@discussions.microsoft.com> schrieb im Newsbeitrag
news:58**********************************@microsof t.com...
Hi,

What is the cleverest way of preventing the user of a program to start more than instance of the program.

regards Jesper.

Feb 21 '06 #2
Frank wrote:
include the following code in the static void Main(string[] args) routine:


Or, rather more succinctly:

bool owner;
using (Mutex mutex = new System.Threading.Mutex(true, "Test", out
owner);
{
if (owner)
{
Application.Run(new MainForm())
}
else
{
System.Environment.Exit(-1);
}
}

The "using" statement takes care of disposing of the Mutex.

Jon

Feb 21 '06 #3
Hello Jon Skeet [C# MVP],

Yep, and @"Global\Test" if we wanna single instance across sessions. Because
for terminal sessions each client gets unique namespace, where our code works

J> Frank wrote:
J>
include the following code in the static void Main(string[] args)
routine:

J> Or, rather more succinctly:
J>
J> bool owner;
J> using (Mutex mutex = new System.Threading.Mutex(true, "Test", out
J> owner);
J> {
J> if (owner)
J> {
J> Application.Run(new MainForm())
J> }
J> else
J> {
J> System.Environment.Exit(-1);
J> }
J> }
J> The "using" statement takes care of disposing of the Mutex.
J>
J> Jon
J>
---
WBR,
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
Feb 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Brad Tilley | last post: by
4 posts views Thread by Chad Myers | last post: by
11 posts views Thread by Clark Stevens | last post: by
8 posts views Thread by gavinstone007 | last post: by
4 posts views Thread by cnixuser | last post: by
1 post views Thread by aj | last post: by
1 post views Thread by =?Utf-8?B?UGF1bCBIYXRjaGVy?= | last post: by
reply views Thread by rosydwin | last post: by

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.