none
How to prevent running or executing of an application when it is already executing. RRS feed

  • Question

  • Folks, How to prevent a 2nd run or execution of an application when it is already executing.

    Thank you

    Monday, October 25, 2010 11:13 PM

Answers

  • Hi,

    Thanks for your post. Jacky Kwok got the point. Please take a look at the following article. It demonstrates how to use a system mutex object to create a singleton application in C#. That program use a named sytem mutex object to detect if the program is already running. But it has some minor problem. In the IsAlreadyRunning() method, the mutex object is named after the program name, and the program name is retrieved at runtime. So if the program name is changed, another instance of the program can still be launched. This can be easily corrected, just use some uniuqe name instead of the program name, a GUID is an option.

    Single Instance Application in C#


    Please mark the right answer at the right time.
    Thanks,
    Sam
    • Marked as answer by Diana4 Tuesday, October 26, 2010 6:13 AM
    Tuesday, October 26, 2010 2:45 AM

All replies

  • Hi:

     

    Most solution will use a named system object, e.g. named event.

     

    When an app is started , it tries to open an named event with an unique name.

    if the event does not exist, the app creates the event with the unique name, then it is the first instance of the app.

    if app can open the existing event, then the app is not the first instance of the app. The app should quit itself.

     

    Jacky Kwok

     

    Tuesday, October 26, 2010 1:20 AM
  • The application I am referring to is a C Sharp application and I am unable to start a new instance of it, everytime I double click on its .exe. I want that not to happen. Thanks

    Tuesday, October 26, 2010 1:41 AM
  • Hi,

    Thanks for your post. Jacky Kwok got the point. Please take a look at the following article. It demonstrates how to use a system mutex object to create a singleton application in C#. That program use a named sytem mutex object to detect if the program is already running. But it has some minor problem. In the IsAlreadyRunning() method, the mutex object is named after the program name, and the program name is retrieved at runtime. So if the program name is changed, another instance of the program can still be launched. This can be easily corrected, just use some uniuqe name instead of the program name, a GUID is an option.

    Single Instance Application in C#


    Please mark the right answer at the right time.
    Thanks,
    Sam
    • Marked as answer by Diana4 Tuesday, October 26, 2010 6:13 AM
    Tuesday, October 26, 2010 2:45 AM