0

Possible Duplicate:
Prevent multiple instances of a given app in .NET?
How can I enforce a single instance of my application?

I have a source then complie to App.exe file. I want if App.exe is running, then a person open it again, it know that it is running and close automaticly. Is there any way to do this?

**Edit: I want a code that insert to source of App.exe

Window Form**

Community
  • 1
  • 1
NoName
  • 7,940
  • 13
  • 56
  • 108

1 Answers1

1

You want to use a Mutex, like so:

bool bIsSingleInstance;
using (new Mutex(true, Assembly.GetExecutingAssembly().GetName().Name, out bIsSingleInstance)) {
    if (bIsSingleInstance) {
        // START APP HERE.
    }
}

Note that you must NOT use 'using' if your winforms isn't blocking in the comment block.

Deathspike
  • 8,582
  • 6
  • 44
  • 82