* * *

Author Topic: [solved] prevent multiple instances  (Read 1683 times)

HobbyDev

  • New member
  • *
  • Posts: 9
[solved] prevent multiple instances
« on: March 22, 2017, 09:48:27 pm »
Hello, I am still trying to switch from Delphi to Lazarus. Now I run into an other problem. I want to make sure the application can only run once at a time. With Delphi I used this code:

Code: Pascal  [Select]
  1. var hPrevWin : HWND;
  2.     Mutex    : THandle;
  3.     RegisterError : integer;
  4.     MutexError    : integer;
  5.  
  6. begin
  7.   MyMsg := RegisterWindowMessage('Call_First_Arbeitszeiterfassung_Instance');
  8.   RegisterError := GetLastError;
  9.   Mutex := CreateMutex(nil, True, 'My_Unique_Application_Mutex_Name');
  10.   MutexError := GetLastError;
  11.   if (Mutex = 0) OR (MutexError = ERROR_ALREADY_EXISTS) then
  12.     begin
  13.       SendMessage(HWND_BROADCAST, MyMsg, 0, 0);
  14.       Halt(0);
  15.     end
  16.   Else
  17.     begin
  18.       Application.Initialize;
  19.       Application.Title := 'AZE';
  20.       Application.CreateForm(TForm1, Form1);
  21.       Application.CreateForm(TLogBookDlg, LogBookDlg);
  22.       if MyMsg = 0 then
  23.         Application.Messagebox(PChar(InttoStr(RegisterError)),'Registering Window Message',mb_OK);
  24.       Application.Run;
  25.       if Mutex <> 0 then
  26.         FileClose(Mutex); { *Konvertiert von CloseHandle* }
  27.     end;
  28.  
  29. end.  

But Lazarus does not understand the "RegisterWindowMessage" and "Mutex" stuff. Does anyone has an example how to prevent an application to have multiple instances?

Thanks in advanced!
« Last Edit: March 24, 2017, 08:47:24 pm by HobbyDev »

Zoran

  • Hero Member
  • *****
  • Posts: 1148
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: prevent multiple instances
« Reply #1 on: March 22, 2017, 09:58:52 pm »

HobbyDev

  • New member
  • *
  • Posts: 9
Re: prevent multiple instances
« Reply #2 on: March 22, 2017, 10:09:33 pm »
That looks great! I will check it out! Thank you very much. I just used the wrong words while searching

Fred vS

  • Hero Member
  • *****
  • Posts: 1663
    • miXimum is the DJ's best friend
Re: prevent multiple instances
« Reply #3 on: March 22, 2017, 10:55:02 pm »
Hello.

There is also this one: https://github.com/fredvs/runonce_postit

Fre;D
I use Lazarus 1.8.0 32/64 and FPC 3.0.3 32/64 on Linux Mint Mate 17 32/64, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs

HobbyDev

  • New member
  • *
  • Posts: 9
Re: prevent multiple instances
« Reply #4 on: March 23, 2017, 10:11:05 pm »
Today I tested the "UniqueInstance" Component. In the testraw project I placed the component, defined all the properties and compiled it successfully. But the application itself has a size of 14MB. While trying to start an other instance I get some Error Messages (see attachment). Same Error shows up, when I exit the application via the cross in the upper right corner - not using the "Crash Application" Button

Did I miss anything?

« Last Edit: March 23, 2017, 10:13:10 pm by HobbyDev »

RAW

  • Sr. Member
  • ****
  • Posts: 488
Re: prevent multiple instances
« Reply #5 on: March 23, 2017, 10:33:44 pm »
 :D That's funny...
That is not an error message, it's the simple version of FastMM4 ... :P

Do you recognize the "CHANGE BUILD MODE"-Button?
Just use it to create DEBUG and RELEASE-Mode...


Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.0RC4 FPC 3.0.4

wp

  • Hero Member
  • *****
  • Posts: 3727
Re: prevent multiple instances
« Reply #6 on: March 23, 2017, 10:34:56 pm »
This is not an error. It is just the message that heaptrc did not find any memory leaks.

The only problem is that the testraw project is compiled with the heaptrc option on. Open the "Project Options", "Compiler Options" -> "Debugging" and remove the checkmark of "Use heaptrc unit". It is certainly a left-over of testing the component.

As for the size of program, you should seek the forum for similar questions - it is one of the most-often asked questions here, I don't want to repeat the answer...

To remove the debugger info (which causes this large exe size) you can check "Use external gdb debug symbols file" on the same project options page to bring the exe size down to about 1.5 MB.
« Last Edit: March 23, 2017, 10:37:57 pm by wp »
Lazarus trunk / fpc 3.0.0 / Win32

RAW

  • Sr. Member
  • ****
  • Posts: 488
Re: prevent multiple instances
« Reply #7 on: March 23, 2017, 10:50:54 pm »
If you like to use a MUTEX you can do it like this ...

For example:
Code: Pascal  [Select]
  1. PROGRAM UNIQUEWAVE;
  2.  {$MODE OBJFPC}{$H+}
  3.  
  4.  USES
  5.   Interfaces,
  6.   SysUtils,
  7.   Forms,
  8.   Windows,
  9.   uUNIQUEWAVE;
  10.  
  11.   {$R *.RES}
  12.  
  13.  CONST
  14.   MutexName = 'UNIQUEWAVE MuTeX';
  15.  
  16.  VAR
  17.   hMutex: THandle;
  18.  
  19. BEGIN
  20.  Try
  21.   hMutex:= CreateMutex(Nil, True, MutexName);
  22.  
  23.    If GetLastError = ERROR_ALREADY_EXISTS
  24.    Then
  25.     Begin
  26.      CloseHandle(hMutex);
  27.      Exit;
  28.     End;
  29.  
  30.   Try
  31.    RequireDerivedFormResource:= True;
  32.     Application.Title:= 'UNIQUEWAVE';
  33.     Application.Initialize;
  34.      wndGUI:= TwndGUI.Create(Application); //no need for a TaskbarButton
  35.      wndGUI.Show;
  36.     Application.Run;
  37.   Finally
  38.    If hMutex <> 0
  39.    Then CloseHandle(hMutex);
  40.   End;
  41.  Except
  42.   On E: Exception
  43.   Do wndGUI.Log('MAIN PRG'+sLineBreak+E.ClassName+sLineBreak+E.Message, True);
  44.  End;
  45. END.
  46.  
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.0RC4 FPC 3.0.4

RAW

  • Sr. Member
  • ****
  • Posts: 488
Re: prevent multiple instances
« Reply #8 on: March 24, 2017, 01:03:05 am »
Just in case you didn't know...
If you like small EXE-Files and less RAM usage then you can check out this:

LLCL LightLCL (Windows only)
https://github.com/FChrisF/LLCL
Very easy to use: Download, Extract, Add the path: COMPILER OPTIONS: OtherUnitFiles, Check the used Units: not all LCL-Units are supported...
Done... Compile...

fpGUI (cross platform)
http://fpgui.sourceforge.net/
https://github.com/graemeg/fpGUI
https://github.com/graemeg/fpGUI/blob/master/extras/freetype_windows/freetype.zip

Very easy to use too: Download, Extract, Open ..src\corelib\fpgui_toolkit.lpk (Open Package File), Click on compile.. done
Now you can start a new project and click on PROJECT: Project Inspector: click ADD, click New Requirement and choose fpgui_toolkit, hold LCL or delete it.
The Unit names are different with fpGUI, but there are a lot of examples... and CTRL+Space should help... (for example type: fpg_ and press CTRL+Space) :)

There are three things that come to my mind when I read fpGUI:
First: smaller EXE-Files and RAM usage
Second: Themed programs, same look on all platforms
Third: AGG PAS, TAGG2D, semitransparent pixels (of course there is another very popular library for semitransparent pixels: BGRABitmap).. both libs got a lot of examples.. very nice...

hot hot hot...
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.0RC4 FPC 3.0.4

HobbyDev

  • New member
  • *
  • Posts: 9
Re: prevent multiple instances
« Reply #9 on: March 24, 2017, 08:46:49 pm »
Many Thanks to all. In the first step I got rid of the "Error- Message". Thanks for sample code with Mutex as well!

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus