Recent

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

Sören

  • Newbie
  • Posts: 5
How to use TCustomApplication.SingleInstanceEnabled?
« on: June 03, 2020, 08:52:28 pm »
Hi there,
I want to allow only one instance of my application.
I tried adding
Code: Pascal  [Select][+][-]
  1. Application.SingleInstanceEnabled:=true;
in the project's .lpr file, but I also have to set a SingleInstanceClass to Application.SingleInstance and that's where I got stuck.

The only source of information that I found was the docs : https://lazarus-ccr.sourceforge.io/docs/fcl/custapp/tcustomapplication.singleinstanceenabled.html
Any help would be appreciated.


jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: How to use TCustomApplication.SingleInstanceEnabled?
« Reply #1 on: June 03, 2020, 10:57:03 pm »
I could be wrong but I believe you do not need to set the class, it already has a default one in there..

 Try first reading the SingleInstance property in the main file and if not set then you can define SingleInstanceEnabled := True;

 If it is already defined then you Skip the remainder of the Application code and just exit.

 That is going by what I have seen in the help file..

 Its not totally automatic because it will allow you to do things differently with multiple instances instead of just terminating automatically
The only true wisdom is knowing you know nothing

Sören

  • Newbie
  • Posts: 5
Re: How to use TCustomApplication.SingleInstanceEnabled?
« Reply #2 on: June 03, 2020, 11:55:38 pm »
Makes sense, but if I try to check Application.SingleInstance I get the error:

Quote
No single instance provider class set! Include a single-instance class unit such as advsingleinstance

and I didn't find any advsingleinstance unit. 

In the fcl-base/custapp.pp file the error is raised in the private function GetSingleInstance when the single instance class is nil.
Unfortunately I only use Lazarus occasionally, so I could be missing something really obvious.
« Last Edit: June 04, 2020, 12:01:59 am by Soeren »

tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: How to use TCustomApplication.SingleInstanceEnabled?
« Reply #3 on: June 04, 2020, 12:58:21 am »
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   AdvancedSingleInstance, // !!!
  10.   Dialogs,
  11.   Interfaces,  // this includes the LCL widgetset
  12.   Forms, Unit1;
  13.  
  14. {$R *.res}
  15.  
  16. begin
  17.     Application.Initialize;
  18.  
  19.     Application.SingleInstanceEnabled := true;
  20.     Application.SingleInstance.Start;
  21.     if Application.SingleInstance.IsServer then
  22.     begin
  23.         Application.CreateForm(TForm1, Form1);
  24.         Application.Run;
  25.     end
  26.     else
  27.         ShowMessage('Only one instance allowed!');
  28. end.
  29.  

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: How to use TCustomApplication.SingleInstanceEnabled?
« Reply #4 on: June 04, 2020, 01:00:36 am »
Makes sense, but if I try to check Application.SingleInstance I get the error:

Quote
No single instance provider class set! Include a single-instance class unit such as advsingleinstance

and I didn't find any advsingleinstance unit. 

In the fcl-base/custapp.pp file the error is raised in the private function GetSingleInstance when the single instance class is nil.
Unfortunately I only use Lazarus occasionally, so I could be missing something really obvious.
you can find the advanced single instance unit in
<Lazarus Install Folder>\fpc\3.0.4\source\packages\fcl-base\src\advancedsingleinstance.pas

I guess in your application you need to add the fcl-base package in your required packages.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: How to use TCustomApplication.SingleInstanceEnabled?
« Reply #5 on: June 04, 2020, 01:09:30 am »
You can use FindWindow('Window', 'Your Unique Window Caption');

If that returns the Handle of the form then to increase the check point you could then
SendMessage(TheHandleIGot, WM_USER+100, 1,0);

and your form should be processing this message and thus return with a apply.

This of course only works in windows and for the life of me I can't figure out why they didn't create the window using the TFORM name class instead of window because this weakens the attempt to find the exact one..

The only true wisdom is knowing you know nothing

Sören

  • Newbie
  • Posts: 5
Re: How to use TCustomApplication.SingleInstanceEnabled?
« Reply #6 on: June 04, 2020, 09:16:47 am »
Many thanks @tetrastes for the sample code - problem solved!
In my case I only had to add the AdvancedSingleInstance unit, but I wasn't aware of .Start and .IsServer either, so your example made everything clear.

Thank you all for your help!
« Last Edit: June 04, 2020, 09:41:41 am by Sören »

 

TinyPortal © 2005-2018