Recent

Author Topic: Restart application  (Read 7712 times)

Emerson

  • New Member
  • *
  • Posts: 37
Restart application
« 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?

Emerson

  • New Member
  • *
  • Posts: 37
Re: Restart application
« Reply #1 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

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Restart application
« Reply #2 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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Ten_Mile_Hike

  • New Member
  • *
  • Posts: 44
Re: Restart application
« Reply #3 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

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Restart application
« Reply #4 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.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Lulu

  • Full Member
  • ***
  • Posts: 226
Re: Restart application
« Reply #5 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...
wishing you a nice life

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Restart application
« Reply #6 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.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Emerson

  • New Member
  • *
  • Posts: 37
Re: Restart application
« Reply #7 on: December 02, 2021, 01:15:50 am »
Thanks everyone for your help, I will test them all and answer the result here.

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: Restart application
« Reply #8 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
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Restart application
« Reply #9 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.

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Restart application
« Reply #10 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.

The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018