7

It should not be possible to run multiple instances of my application. Therefore the project source contains:

CreateMutex (nil, False, PChar (ID));
if (GetLastError = ERROR_ALREADY_EXISTS) then
  Halt;

Now I want to restart my application programmatically. The usual way would be:

AppName := PChar(Application.ExeName) ;
ShellExecute(Handle,'open', AppName, nil, nil, SW_SHOWNORMAL) ;
Application.Terminate;

But this won't work in my case because of the mutex. Even if I release the mutex before starting the second instace it won't work because shutdown takes some time and two instance cannot run in parallel (because of common resources and other effects).

Is there a way to restart an application with such characteristics? (If possible without an additional executable)

Thanks in advance.

4
  • why re-start - just activate the already running instance
    – macf00bar
    Dec 21, 2010 at 10:46
  • 1
    how is that the same? The running instance is already active and I want it to be restarted. Dec 21, 2010 at 11:02
  • 1
    Have you ever tested the return of 'ReleaseMutex'? Dec 21, 2010 at 13:18
  • Releasing is not what you need to do to the mutex. Close it instead. Dec 21, 2010 at 17:03

9 Answers 9

15

Perhaps you should think outside the box. Instead of futzing with the mutex / instance logic, you could simply create another executable that waits for your app to close then starts it again. As an added bonus, you can later use this mechanism to, for example, update some of your main app's binaries. It's also much easier to run it elevated instead of maintaining different integrity levels inside the same app, etc.

1
  • +1; This is a mechanism that a lot of applications do to perform updates. Dec 21, 2010 at 13:57
2

Why can't you just release the mutex before attempting to restart? If by some chance another instance gets going before the one you explicitly invoke with the restart that doesn't matter, you'll still have your app up and running again with whatever changes effected that required the restart. I don't think you need any of the complexity of the other solutions.

10
  • Because for some limited time two instances will run in parallel (shutdown takes some tiem). That's what I want to avoid. Dec 21, 2010 at 10:53
  • so you need to shutdown and re-start or would re-activating the already running app be enough (take a look at the linke I've posted)?
    – macf00bar
    Dec 21, 2010 at 10:56
  • @Smasher in that case do all the shutdown stuff that you need to do in single instance mode, and then after all the shutdown work is done, release the mutex and call ShellExecute. Dec 21, 2010 at 10:58
  • @Smasher All you need to do is put the ShellExecute after the existing mutex release. Presumably you are already doing all the shutdown work before releasing your existing mutex? Dec 21, 2010 at 11:23
  • @David: No I don't and it's not that easy. But I guess you are right that this is the easiest solution. Dec 21, 2010 at 11:26
1

Include in your ShellExecute some parameter, for example, /WaitForShutDown and create one more mutex. In your program, before the initialization, for example, in its .dpr file, insert something like:

if (Pos('/WaitForShutDown', CmdLine) <> 0) then WaitForSingleObject(ShutDownMutexHandle, INFINITE);

Also, in your program, after all the finalizations and releasing your common resources, include something like

ReleaseMutex(ShutDownMutexHandle);

2
  • Thanks! But ShutdownMutexHandle will be undefined at program start. How would you define it? CreateMutex will fail with ERROR_ALREADY_EXISTS error... Dec 21, 2010 at 11:01
  • okay I guess I found it: OpenMutex should be the right thing to call to obtain the handle. Dec 21, 2010 at 11:25
1

EDIT...

OK. Now I belive that I know where is your problem... You have problems with program units finalization!

Try to add at program section as first unit my bottom RestartMutex unit.

program MyProgramName;  
uses
  Mutex,
  Forms,
...

;

unit RestartMutex;
interface

var
  Restart: boolean = false;

implementation

uses
  windows,
  ShellApi;

var
  MutexHandle: cardinal;
  AppName: PChar;
const
  ID = 'MyProgram';

initialization
  MutexHandle := CreateMutex (nil, False, PChar (ID));
  if (GetLastError = ERROR_ALREADY_EXISTS) then
    Halt;

finalization
  ReleaseMutex(MutexHandle);
  if Restart then
  begin
    AppName := PChar('MyProgramName.exe') ;
    ShellExecute(0,'open', AppName, nil, nil, SW_SHOWNORMAL) ;
  end: 
end.

When you want to restart application just set variable Restart to true and than terminate an application.

So, because is RestartMutex added as first in program section, this will couse that finalisation of unit RestartMutex will hepped nearly at the end of closing an application and all other units will do finalization before unit RestartMutex, that mean the Application can start safe again!

2
  • "Even if I release the mutex before starting the second instace it won't work because shutdown takes some time and two instance cannot run in parallel" ... from the question. Dec 21, 2010 at 11:27
  • @Smasher: You have problems with program finalization! Chech my improved answer!
    – GJ.
    Dec 21, 2010 at 21:31
0

You could pass a command line argument like "restart" and run a Sleep() before you try to acquire the Mutex or try to acquire the mutex in a loop that sleeps a while.

Also you could set up communication between both processes, but that might be overkill.

1
  • 2
    The problem with the Sleep is that there is no way to find an appropriate value. Totally depends on the computer, the loaded data and other stuff. Dec 21, 2010 at 10:57
0

hi take a look a the following article by Zarko Gajic - there you will get some ideas, sample code and even a whole component to use.

hth, reinhard

1
0

Your ReleaseMutex is probably failing since you're passing 'False' for 'bInitialOwner' while calling CreateMutex. Either have the initial ownership of the mutex, or call CloseHandle instead of 'ReleaseMutex' passing your mutex handle.

1
  • 2
    No, don't worry about releasing the mutex at all. Ownership of the mutex is completely irrelevant. What you need to do is to destroy the mutex with CloseHandle. That's all. (This of course requires storing the return value from CreateMutex.) Dec 21, 2010 at 17:01
0

checkout this way:

Simply runs a new application and kills the currernt one;

http://www.delphitricks.com/source-code/windows/restart_the_own_program.html

1
  • 1
    To avoid broken links post the relevant information contained in the link here. Feb 11, 2015 at 20:35
0

(beating the sleep idea)

if you want to make sure the original process is really terminated/closed before you create the mutex, then one idea is to pass the PID to the new process (command line is the easiest, any other IPC method works as well), then use OpenProcess(SYNCHRONIZE, false, pid) and WaitForSingleObject (I'd use a loop with a timeout (100 ms is a good value) and act accordingly if the original process takes too long to close)

What I ended up doing, beside the above, was to also create a RestartSelf procedure in the same unit with the mutex, and do the logic there, in order to keep the single instance and restart logic in the same place (the parameter being hardcoded, you don't want hardcoded stuff to be scattered around your application(s).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.