Prevent a second instance from starting, except in a specific case

Ok, I have this program and I don't want more than one instance to run it. So now I have all the instances that match that name, and if there are more than one left, and lets the user know that it is already running in another instance.

However, there is a special case where a new instance wants to change what the other instance is doing, then it exits. How can i do this?

Here's an example: I have this program that times an event. I don't want multiple operations to be done at the same time, because you cannot do two things at once (for this application). Now tell the third party program wants to notify this thread that the user is now working on something else, so he starts the application again. If the application is already running, it will update its actions, otherwise it will act as usual.

How can I do that?

This is what I use to determine if another instance is running:

            string proc = Process.GetCurrentProcess().ProcessName;

            Process[] processess = Process.GetProcessesByName(proc);
            if (processess.Length > 1) {
                MessageBox.Show("There is an instance of the Timer already running");
                return;
            }

      

+1


source to share


3 answers


What complicates this is the fact that under certain conditions you want the second call to the program to do something if the other works. Using a named mutex will allow you to determine if the program is running - it should already contain mutexes. You still need a way to communicate with it to tell the executable to do something when the second starts up. An asynchronous message queue will probably work, you just need to have the running program check it periodically to see if new messages are pending. A message will need to tell the program how to change. Have a look at the System.Threading namespace (as it looks like you are using .Net already), specifically the mutex and semaphore classes, and System.Messaging.MessageQueue for messaging.

Main idea:



  program start
  try to acquire mutex (or semaphore)
  if failed
     send message via message queue to running program
     exit
  else

  set up listener for message queue

  run rest of program

      

The listener can take the form of a timer that periodically expires with an expiration callback for messages in the queue and adjusts the program accordingly. The timer must be autoset so that it returns to listening. Your program will need to be able to recover from a timer interrupt and "reboot" based on the updated configuration.

+2


source


You can use a generic (named) mutex for this.



See the Win32 API Documentation for Mutex Objects (I assume there are language bindings for C # for this).

+1


source


Your best bet is to use Mutex for discovery and then use very trivial remote access via IPCChannel.

All it takes is 1 MarshalByRefObject class and the remote access services start when the application is first started.

0


source







All Articles