Register  |  Login



Earn money by sharing your knowledge through Quomon's revenue sharing program

Question

Status: Closed Points: 125 Time: 13:06 - Jul 03, 2007  

theDude

From a windows .net application is it possible to detect if the application is already running?

I am developing a windows .net program (in c#) and I was wondering if it is possible to detect on startup if the program is already running, so that the user is not allowed to run two instances of it at once?
How is that done?

Answer Discussion
Tutorials

 

Q&A System for Websites and Corporate Collaboration

Advertisement

  • Generates significant organic traffic for websites
  • Saves companies money, resources, and time

PeterNZ

Date:: Jul 03, 2007

Time:: 15:25

Yes, this is possible. You have to use System.Threading.Mutex. This is a system wide handle for an application. A mutex has a system wide mutex name i.e. "MYAPPLICATION". You can check for the mutex and if it exists, you know that your application is already running and you can react accordingly. Here is an example from a book:

using System.Threading;

class Program
{
static void Main(string[] args)
{
Mutex oneMutex = null;
const string MutexName = "RUNMEONLYONCE";

try // Try and open the Mutex
{
oneMutex = Mutex.OpenExisting(MutexName);
}
catch (WaitHandleCannotBeOpenedException)
{
// Cannot open the mutex because it doesn't exist
}

// Create it if it doesn't exist
if (oneMutex == null)
{
oneMutex = new Mutex(true, MutexName);
}
else
{
// Close the mutex and exit the application
// because we can only have one instance
oneMutex.Close();
return;
}

Console.WriteLine("Our Application");
Console.Read();
}
}

Let me know if this helped you!

Cheers

Peter

theDude

Date:: Jul 05, 2007

Time:: 03:01

Thanks, Peter. this is perfect!

Prajith

Date:: Jul 24, 2007

Time:: 13:26

For VB.NET take Project Properties --> Enable Application Framework --> Windows Application Framework Properties --> Check in Make Single Instance Application

Question Answered

This question has been closed, and points have been rewarded to the following experts:

PeterNZ: 125

You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

No tutorials have been submitted yet. Want to be the first?

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top windows Experts

View More

Rank

Expert

Points

1.

nidhi

942

2.

PeterNZ

410

3.

oracleofDelphi

388

4.

PatTheDBA

125

5.

rcastagna

99

6.

bob_man_uk

50

7.

multani.sarbjit

50

8.

jgivoni

45

9.

TheFormatter

38

10.

LonelyWolf

37

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es