jpfollenius
jpfollenius

Reputation: 16620

Restart Delphi Application Programmatically

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.

Upvotes: 7

Views: 12295

Answers (9)

ciuly
ciuly

Reputation: 552

(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).

Upvotes: 0

marceldanilo
marceldanilo

Reputation: 1

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

Upvotes: 0

GJ.
GJ.

Reputation: 10937

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!

Upvotes: 1

Mihai Limbășan
Mihai Limbășan

Reputation: 67726

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.

Upvotes: 15

Sertac Akyuz
Sertac Akyuz

Reputation: 54812

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.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 613262

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.

Upvotes: 2

macf00bar
macf00bar

Reputation: 700

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

Upvotes: 0

Eldar Isayev
Eldar Isayev

Reputation: 31

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);

Upvotes: 1

Jens M&#252;hlenhoff
Jens M&#252;hlenhoff

Reputation: 14873

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.

Upvotes: 0

Related Questions