Recent

Author Topic: How to use TCustomApplication.SingleInstanceEnabled?  (Read 2425 times)

Jonny

  • Full Member
  • ***
  • Posts: 144
Re: How to use TCustomApplication.SingleInstanceEnabled?
« Reply #15 on: February 18, 2025, 09:50:42 am »
Quote from: Zvoni
As i said: Never worked with it, just deriving what i read from the Source-Code

Thank you Zvoni, your insightfulness is very helpful. It is good to know that you are just reading the source code too. After Thaddy's reply I was really doubting myself if there was any documentation that I was unaware of. Once I get it working then shall post the code here to serve as reference for others.

One last question, is there a cross-platform command to bring an application into focus and to the front? I tried the BringToFront (via tcpip tests) but it did nothing, I assume that it works for its own windows and not systemwide. A minimise then maximise has better results but is ugly.

dbannon

  • Hero Member
  • *****
  • Posts: 3297
    • tomboy-ng, a rewrite of the classic Tomboy
Re: How to use TCustomApplication.SingleInstanceEnabled?
« Reply #16 on: February 18, 2025, 11:17:50 am »
Jonny, another possibility is the SimpleIPC client and server. Examples at <your-fpc-source>packages/fcl-process/examples/

Possible the .SingleInstance thing you ask about is based on it. When your app starts, try to send a message to the server, if you succeed then you know to exit. If no server present, become server. Once its in place, you may find other uses for that message sending ability. I did.

Showing the running instance ?  Here is what I do ....

Code: Pascal  [Select][+][-]
  1. procedure TSearchForm.MoveWindowHere(Frm : TForm);
  2. begin
  3.     if Frm = Nil then exit;
  4.     if not Frm.Visible then Frm.Show;
  5.     {$ifdef LINUX}               // On Linux, the target form may be in another workspace
  6.     if not mvxwindow.MvXWinWorkSpace(Frm.Caption) then                  // drops message to console if fails
  7.     begin                        // Normally, this seems highly unlikely ! Maybe Enlightenment DE ?
  8.         Frm.Hide;
  9.         Frm.show;                // This produces a noticable flicker if the form is already visible
  10.     end;
  11.     {$else}
  12.     Frm.EnsureVisible(True);     // All that is necessary in a non-workspace system
  13.     {$endif}
  14. end;

I have decided the flicker when Hide-Show is a good thing, it attracts the user's attention.

Davo

PS, a polite response to Thaddy ? Now, thats a novel idea !
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

Jonny

  • Full Member
  • ***
  • Posts: 144
Re: How to use TCustomApplication.SingleInstanceEnabled?
« Reply #17 on: February 18, 2025, 01:50:18 pm »
Quote from: dbannon
Jonny, another possibility is the SimpleIPC client and server. Examples at <your-fpc-source>packages/fcl-process/examples/

Possible the .SingleInstance thing you ask about is based on it. When your app starts, try to send a message to the server, if you succeed then you know to exit. If no server present, become server. Once its in place, you may find other uses for that message sending ability. I did.

Thanks Davo, indeed AdvancedSingleInstance uses AdvancedIPC so your idea will work. But AdvancedSingleInstance is maintained as part of the FPC source so I did not want to "reinvent the wheel" if possible.

Quote from: dbannon
Showing the running instance ?  Here is what I do ....

Code: Pascal  [Select][+][-]
  1.     {$ifdef LINUX}               // On Linux, the target form may be in another workspace
  2.     if not mvxwindow.MvXWinWorkSpace(Frm.Caption) then                  // drops message to console if fails
  3.     begin                        // Normally, this seems highly unlikely ! Maybe Enlightenment DE ?
  4.         Frm.Hide;
  5.         Frm.show;                // This produces a noticable flicker if the form is already visible
  6.     end;
  7.     {$else}
  8.     Frm.EnsureVisible(True);     // All that is necessary in a non-workspace system
  9.  

I have decided the flicker when Hide-Show is a good thing, it attracts the user's attention.


Thanks for confirming, it seemed like a hack, glad it was not just me. Is just Linux a special case or macOS too?

Quote from: dbannon
PS, a polite response to Thaddy ? Now, thats a novel idea !

I love Thaddy, he was my mentor (the KOL guru) when I was learning, so I will always be polite to him (and to all!). He just needs a nudge sometimes (I suspect that he did not read the link that he provided which would negate the RTFM statement). There are vast swaths of valuable knowledge in his head which could benefit all, I still await his reply.

Jonny

  • Full Member
  • ***
  • Posts: 144
Re: How to use TCustomApplication.SingleInstanceEnabled?
« Reply #18 on: February 18, 2025, 02:52:38 pm »
OK, I found the issue.

SingleInstance only works with with TCustomApplication. It does not work with TApplication which makes it useless for GUI applications since it cannot CreateForm (even if cast to TApplication).

Also, assigning the OnServerReceivedCustomRequest event handler causes the CPU usage to shoot up to 100% and remain there - therefore useless for console applications too.

Unless anyone has a fix then I shall take Davo's advice and look at SimpleIPC.

Zvoni

  • Hero Member
  • *****
  • Posts: 2914
Re: How to use TCustomApplication.SingleInstanceEnabled?
« Reply #19 on: February 18, 2025, 03:37:52 pm »
OK, I found the issue.

SingleInstance only works with with TCustomApplication. It does not work with TApplication which makes it useless for GUI applications since it cannot CreateForm (even if cast to TApplication).

Also, assigning the OnServerReceivedCustomRequest event handler causes the CPU usage to shoot up to 100% and remain there - therefore useless for console applications too.

Unless anyone has a fix then I shall take Davo's advice and look at SimpleIPC.
That's also my own conclusion.
TCustomApplication checks in its DoRun-Procedure the Server-Messages, the TApplication in a GUI doesn't
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

 

TinyPortal © 2005-2018