Recent

Author Topic: [CLOSED] UniqueInstance  (Read 1475 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[CLOSED] UniqueInstance
« on: March 26, 2022, 06:24:41 pm »
I use uniqueinstance to allow only one running instance of a program. This works without issue. Does anyone know, or have an idea how, I can use  this to close the existing program and run the new copy?
« Last Edit: March 28, 2022, 11:39:41 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: UniqueInstance
« Reply #1 on: March 26, 2022, 09:15:04 pm »
You can use a mechanism like Lazarus itself uses.
When new packages are compiled into Lazarus by user interaction (if you accept the dialog shown) it closes itself and runs a separate starter program which in turn re-runs Lazarus.

korba812

  • Sr. Member
  • ****
  • Posts: 482
Re: UniqueInstance
« Reply #2 on: March 26, 2022, 09:54:37 pm »
Lazarus needs starter because it replaces the executable on rebuild. If you don't need to replace the executable, just add a comand line parameter which will ignore "UniqueInstance" then run your program with this parameter (using ShellExecute or TProcess) and terminate original application.

dbannon

  • Hero Member
  • *****
  • Posts: 3612
    • tomboy-ng, a rewrite of the classic Tomboy
Re: UniqueInstance
« Reply #3 on: March 27, 2022, 12:33:20 am »
I used uniqueinstance in my app and while it generally worked OK, I found that if the app crashed, it would sometimes continue reporting itself as OK but not otherwise responding. This was a particular problem on the Mac where it was sometimes very hard to clean up after a crash.

So, instead, I now use the https://wiki.freepascal.org/SimpleIPC model, its heaps more reliable and much more flexible, you define what messages you want to pass  between processes, very easy to add a message that says "OK you, close now !".

And it obviously solves the unique problem as well, as the app starts up, it tries to find another SimpleIPC server, if not found, it becomes the server.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: UniqueInstance
« Reply #4 on: March 27, 2022, 06:01:33 am »
I used uniqueinstance in my app and while it generally worked OK, I found that if the app crashed, it would sometimes continue reporting itself as OK but not otherwise responding. This was a particular problem on the Mac where it was sometimes very hard to clean up after a crash.

Why would you use it on a Mac which automatically only runs one instance of an application? (unless you override it with "open -n")

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: UniqueInstance
« Reply #5 on: March 27, 2022, 11:56:23 am »
Thanks all. I think IPC is the way to go.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018