none
C# App that only allows one instance to be live

    Question

  • I wonder if this is possible with C#. I need to write an application that will only allow one instance to be running, but if another instance is attempted to start it will pass the parameters to the first instance and not continue.

     

    For example, the app is started on command line as: c:\app.exe.

     

    Then, another instance is attempted to start as: c:\app.exe -Lfile.ext. When this instance is tried to start, it will pass the -Lfile.ext parameter to the first instance and shutdown.

     

    Is it possible in C# or will I have to use C++?

     

    I apologize if this isn't the correct forum - I'm a noob.

     

    Thanks,

    David

     

    Tuesday, September 04, 2007 2:12 PM

Answers

  • It's relatively easy to find another instance and activate it. It is somewhat more tricky to pass parameters to another instance that is already running.

    If it's already running, it could be doing anything, like be busy in a loop or displaying a message box. There's no way for you to pass the arguments to the Main() function (you'd face an identical problem with a C++ program, of course).

    There are ways to call methods in a running C# program - to find information about that, google for:

    C# remoting

    There are other ways you could do that too - for example, you could send windows messages to the application, or you could put the data into a memory mapped file and use a manual reset event to signal to the app that data is available.

    None of this is trivial, and it is outside the scope of any answer I can give you here. I can only say that it IS possible to do in C#.

    Tuesday, September 04, 2007 2:52 PM

All replies

  • The easiest is to simply use a named Mutex.  For example

    Code Snippet

    class Program
    {
     static Mutex instanceMutex;

     static void Main()
     {
            bool firstInstance;
            m = new System.Threading.Mutex(true, "{EB05DC46-0A2F-4320-A8C1-97914257BC4C}", out firstInstance);

            if (!firstInstance)
            {
         // instance already running, exit.
                return;
            }

            /// Application.Run(...) or whatever you need your program to do...
     }
    }

     

     

     

    Tuesday, September 04, 2007 2:43 PM
  • It's relatively easy to find another instance and activate it. It is somewhat more tricky to pass parameters to another instance that is already running.

    If it's already running, it could be doing anything, like be busy in a loop or displaying a message box. There's no way for you to pass the arguments to the Main() function (you'd face an identical problem with a C++ program, of course).

    There are ways to call methods in a running C# program - to find information about that, google for:

    C# remoting

    There are other ways you could do that too - for example, you could send windows messages to the application, or you could put the data into a memory mapped file and use a manual reset event to signal to the app that data is available.

    None of this is trivial, and it is outside the scope of any answer I can give you here. I can only say that it IS possible to do in C#.

    Tuesday, September 04, 2007 2:52 PM
  • There shouldn't be any issues with the app being in a loop or it being busy other wise though. The application is an audio player. It will be used inhouse (by the company I work for) by users that do transcription work. It will be "controlled", so to speak, by the transcription data entry program and actions performed therein by the user.

     

    For instance, when the user starts up the transcription data entry app, the audio player will be started with no parameters with a "process.start". When the user requests a dictation to transcribe, the controlling app will do a "process.start" on the audio player with the the assigned audio file as a parameter. When the user finishes the transcription work and submits the finished document, the controlling app will do a "process.start" on the audio player with the assigned audio file and another parameter to let the audio player know that it doesn't need that file anymore so it can drop it off the playlist.

     

    Our data entry app already "controls" a 3rd party audio player in the manner I described above. I don't have a clue how that 3rd party developed their app to be able to do what it does. Too many limits on how that 3rd party app can "connect" to the audio files is the reason for developing our own player.

     

    Thanks for the code samples. They will definitely help. I appreciate any help given. Or any ideas as to a better and/or easier way to do this. Smile

     

    Thanks,

    David

     

     

    Tuesday, September 04, 2007 3:53 PM