Lazarus

Programming => General => Topic started by: Emerson on November 17, 2021, 06:39:51 pm

Title: Restart application
Post by: Emerson on November 17, 2021, 06:39:51 pm
Hi!
I need a function that terminates the application and then runs it again.
Does anyone have this algorithm that can help me?
Title: Re: Restart application
Post by: Emerson on November 17, 2021, 06:54:34 pm
You can leave it personal, I found it here. Thank you very much!


https://forum.lazarus.freepascal.org/index.php/topic,26847.msg165406.html#msg165406
Title: Re: Restart application
Post by: MarkMLl on November 18, 2021, 08:27:45 am
Note the caveats about path etc. in that example. In practice you're likely to find that anything like this has OS-specific elements... please remember to ALWAYS say what OSes you're interested in and what FPC/Lazarus version you're running with ANY question.

Having got that out of the way, I'd point out an issue that's probably universal. The "parent" program might be processing files and/or listening for socket connections etc., you might have problems getting a child to inherit all of that particularly since ports (i.e. TCP/UDP) might require a delay before being re-used. I'm not saying that you'll have definite problems there, just highlighting the potential.

MarkMLl
Title: Re: Restart application
Post by: Ten_Mile_Hike on November 27, 2021, 05:16:40 pm
Years ago in Windows ( "7" I think) I used this kludge

1. Upon exiting my application I had my program output a batch file
2. My program executed the batch file.
3. The application finished terminating
4. The now running batch file looped until it found that my program wasn't running any longer
5. My batch file then executed the program that originaly created it
Title: Re: Restart application
Post by: Mr.Madguy on November 27, 2021, 05:29:47 pm
At least on Windows it can't be done without external application. You launch some sort of update program. You should provide some soft of information about your program's process, so update program would be able to wait for it to terminate (like WaitForSingleObject(Process)). Then external program updates your main program, relaunches it and terminates.
Title: Re: Restart application
Post by: Lulu on November 27, 2021, 05:58:39 pm
Hello, just an 2 cents idea...
Application1 launches application2 and waits for it to finish.
Application2 is the real application used by the user.
When application2 needs to restart, it signals it to application1 by a tag in a file for example or by pipe communication, then finishes.
The application1 detects the end of the application2, reads the file or the pipe and if necessary restarts the application2.
This is a bit far-fetched, of course...
Title: Re: Restart application
Post by: Mr.Madguy on November 28, 2021, 06:01:44 am
Hello, just an 2 cents idea...
Application1 launches application2 and waits for it to finish.
Application2 is the real application used by the user.
When application2 needs to restart, it signals it to application1 by a tag in a file for example or by pipe communication, then finishes.
The application1 detects the end of the application2, reads the file or the pipe and if necessary restarts the application2.
This is a bit far-fetched, of course...
This is called launcher. Yeah, it's good solution. Only problem - anti-viruses don't like CreateProccess. And it's needed to wait for application 2 to terminate.
Title: Re: Restart application
Post by: Emerson on December 02, 2021, 01:15:50 am
Thanks everyone for your help, I will test them all and answer the result here.
Title: Re: Restart application
Post by: Zvoni on December 02, 2021, 08:35:29 am
This is called launcher. Yeah, it's good solution. Only problem - anti-viruses don't like CreateProccess. And it's needed to wait for application 2 to terminate.
Then shell it instead of CreateProcess
Title: Re: Restart application
Post by: PascalDragon on December 02, 2021, 08:53:12 am
This is called launcher. Yeah, it's good solution. Only problem - anti-viruses don't like CreateProccess. And it's needed to wait for application 2 to terminate.

Everything that starts a process uses CreateProcess (even ShellExecute uses it under the hood). Anti-viruses merely hook it so that they can scan started binaries before they start running.
Title: Re: Restart application
Post by: jamie on December 02, 2021, 01:45:07 pm
The last time I did this and I believe it still works is I simply restart a copy of itself but I give it a current value of the process that is running on the command line so the next instance can check for this and then sit in a wait loop at the main start of the app until it closes.
 
 Of course, you also need to wait for the new process to register from the first one before you can assume it's ok to terminate.

 There are ways of doing this without using a proxy app.

TinyPortal © 2005-2018