Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to restart application within itself when at a time only one instance of an application is allowed to run?
Posted
Comments
Mohibur Rashid 25-Apr-13 4:54am    

If all you want is a restart then you can do the following:
Use GetCurrentProcess() to get a PSEUDO HANDLE to the current process, and duplicate it with DuplicateHandle() in order to get a non-pseudo INHERITABLE handle. When this is done you can use GetModuleFileName(NULL, buf, bufsize) to get the path to the current executable. You should launch the exe with CreateProcess() with bInheritHandles==TRUE parameter so the next instance you start will be able to inherit the duplicated handle so that the launched process will have a valid handle with the same numeric value referencing the original process object in the kernel. When you start the next process with CreateProcess(bInheritHandles==TRUE) you should pass the numeric value of the duplicated process handle to the child process for example as a commandline parameter ("x\y\process.exe --wait_exit 2345325"). After launching the child process you can close the duplicated handle with CloseHandle(), cleanup everything and then quit.
At startup you should handle the "--wait_exit" commandline parameter and if it is present on the command line then you should convert the parameter into an int and then cast into a HANDLE then wait for it with WaitForSingleObject(). When the wait is finished you can close the handle with CloseHandle(). This way your child process waits until the original process is done.

After waiting with WaitForSingleObject() you can add some extra code to ensure that only one instance is running at a time - I usually use a named event object for that.

If you want to replace the executable file during restart then you should do almost the same but instead of starting the same exe, you have to start another helper exe (updater.exe). This way the updater.exe can wait till the original exe terminates, can replace the exe without trouble and then it can launch the new exe.
 
Share this answer
 
Comments
nv3 25-Apr-13 15:08pm    
My 5. Good material in there.
pasztorpisti 26-Apr-13 2:46am    
Thank you! This is just one possible solution and real life is a bit more complicated and cruel than I described. I remember when last time I wrote an updater with this technique, Replacing the exe file wasn't as slick as you would think. After waiting for the process terminate the exe wasn't always writable (sometimes explorer.exe held an open handle, sometimes another process) for a few tenth of seconds or seconds and I wrote a loop of a few retries with half second delay between them...
nv3 26-Apr-13 3:13am    
I fully agree. Having fought with the forces of nature and Windows a couple of times myself I know exactly what you mean.
If only one instance of an application is allowed to run then see this Single-Instance C# Application - for .NET 2.0[^] which also explains how to pass command=line changes to the instance that is already running.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900