views:

273

answers:

2

I have a console application that is server based. I only want 1 instance of it running at once for a particular server (this is regardless of the user who might be running it).

I need to add a check to make sure only 1 instance of it is running, I can already do this by checking the running processes on the server, but is this best practice?

Since I am constantly looking for ways to improve coding styles and stay up to date, is there a better way to do this lately? If you're thinking - "If it ain't broke don't fix it", Maybe you're right, but I want to take more advantage of framework built in functionality.

I am using .net v3.5, and this is a console application.

Thanks in advance

+7  A: 

You should use Mutex class, like explained here: C# .NET Single Instance Application

Rubens Farias
+1 and accepted answer, not sure if you can get a better answer than this. tyvm!
JL
+1 I agree with JL
Vitaliy Liptchinsky
Well that took a whole 2 minutes to implement :)
JL
nice to hear that =)
Rubens Farias
One thing I would like to add, I notice the mutex has a guid, does this mean that if a stubborn admin renames the executing exe, it still wont run because the mutex guid is the same in both applications, even the renamed one?
JL
Answer = Yes :)
JL
That's basically just a string identification code you pick, it can be a guid, or the original full name of your project, or whatever, it just has to be unique so that you won't collide with other applications.
Lasse V. Karlsen
+2  A: 

What is the correct way to create a single instance application has some ways to do this, including the article mentioned above

SwDevMan81