Difference between revisions of "UniqueInstance"

From Lazarus wiki
Jump to navigationJump to search
 
(32 intermediate revisions by 11 users not shown)
Line 1: Line 1:
 +
{{UniqueInstance}}
 +
== Description ==
 
=== About ===
 
=== About ===
UniqueInstance provides an easy way to force only one instance per application running at same time.
+
UniqueInstance provides an easy way to force only one instance per application running at same time. To install this component on your component palette, the easiest way is to use the Lazarus [[Online Package Manager]]. Supports Windows and Linux (macOS prevents multiple instances by default).
 
 
  
 
=== Features ===
 
=== Features ===
Line 9: Line 10:
 
=== How To Use (Component) ===
 
=== How To Use (Component) ===
  
Just drop in the main form
+
Just drop in the main form of a LCL application (it's on the System tab, look for a red full-circle with the digit "1" in the middle).
  
 
Properties:
 
Properties:
Line 15: Line 16:
 
* Enabled: enables/disables the component
 
* Enabled: enables/disables the component
 
* Identifier: used to provides a way to identify the application
 
* Identifier: used to provides a way to identify the application
 +
* UpdateInterval: the interval in milliseconds which the component will monitor messages from new instances. <b>Has meaning only under unix since, under win32, the message is received through the windows message loop</b>
  
 
Event
 
Event
Line 21: Line 23:
 
=== How To Use (Raw) ===
 
=== How To Use (Raw) ===
  
Add uniqueinstanceraw unit to your uses section.
+
Add <b>uniqueinstanceraw</b> unit to your uses section.
  
Call the InstanceRunning function: it will return true if there's already a instance running. There are two variants: one with an Identifier argument (String) and one without. The second will use the executable name as the Identifier.
+
Call the <i>InstanceRunning</i> function: it will return true if there's already a instance running.  
  
 +
There are two variants:
 +
* Without arguments: will use the executable name as identifier and will not send the command line parameters
 +
* With two arguments:
 +
** Identifier: the identifier of the application
 +
** SendParameters(default = false): send the command line parameters to the already running instance, if any, before quit.
  
 
=== Remarks ===
 
=== Remarks ===
* It was developed and tested under win32. I don't have a Linux machine at time
+
* Tested with win32 (XP SP2) and Linux (Ubuntu 9.04) with fpc 2.4.2.
* Although it does not use platform dependent code, the callback mechanic used to pass the command line probably won't work in Linux. This occurs because TSimpleIPCServer under Linux does not call the OnMessage event (At least until some months ago when i tested it).
+
* If you put two TUniqueInstance components in the same application with the same identifier your application won't load.
* If you put two TUniqueInstance components in the same application with the same identifier your application wont load.
+
* The Identifier is optional both to the function and the component. In the case it's not used, the executable name is used as an Identifier. The consequence is that if someone changes the exe name another instance will not be identified. In the other hand, if two different applications use the same Identifier one will prevent the other from launching
* The Identifier is optional both to the function and the component. In the case it's not used, the executable name is used as a Identifier. The consequence is that if someone changes the exe name another instance will not be identified.
+
** TIP: to make sure an application will not prevent another from launching you can use a GUID as the Identifier
* To make sure a application will not avoid other launching you can use a GUID as the Identifier
+
 
 +
=== History ===
 +
* 11/01/17 - Version 1.1
 +
** Make compatible with fpc 3.0 and above
 +
** Code cleanup
 +
* 17/04/11 - Version 1.0
 +
** Fix application being detected as running after a crash under unix
 +
** Fix crash when compiling with Gtk2 widgetset
 +
** Set default values of published properties
 +
** Optimizations and code clean up
 +
** New icon
 +
* 02/10/07 - Version 0.2
 +
** Based in fpc 2.2.0
 +
** Implemented parameter reception under unix
 +
** General optimizations and code clean up
 +
* 16/12/06 - Initial release. See [http://lazarusroad.blogspot.com/2006/12/only-one-instance.html my blog] to know how it began.
  
 
=== Author ===
 
=== Author ===
  
Luiz Américo Pereira Câmara
+
[[User:Luizmed|Luiz Américo Pereira Câmara]]
 
 
  
 
=== License ===  
 
=== License ===  
Line 45: Line 66:
 
=== Download ===
 
=== Download ===
  
[http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=215290 Lazarus CCR sourceforge].
+
Version 1.1: [https://github.com/blikblum/luipack/releases/tag/uniqueinstance-1.1 Download from GitHub]
 +
 
 +
Recent development moved to GitHub repository: https://github.com/blikblum/luipack/tree/master/uniqueinstance
 +
 
 +
You can use SVN client:
 +
  <nowiki>svn co https://github.com/blikblum/luipack.git/trunk/uniqueinstance</nowiki>
 +
 
 +
== See also ==
 +
* [[AppIsRunning]]
 +
 
 +
[[Category:Components]]

Latest revision as of 00:37, 19 June 2020

Deutsch (de) English (en) español (es) français (fr) polski (pl)

Description

About

UniqueInstance provides an easy way to force only one instance per application running at same time. To install this component on your component palette, the easiest way is to use the Lazarus Online Package Manager. Supports Windows and Linux (macOS prevents multiple instances by default).

Features

  • Easy of use: just drop a component in the main form
  • Provides a mechanism to receive the Command Line of the other instances

How To Use (Component)

Just drop in the main form of a LCL application (it's on the System tab, look for a red full-circle with the digit "1" in the middle).

Properties:

  • Enabled: enables/disables the component
  • Identifier: used to provides a way to identify the application
  • UpdateInterval: the interval in milliseconds which the component will monitor messages from new instances. Has meaning only under unix since, under win32, the message is received through the windows message loop

Event

  • OnOtherInstance: called when another instance is initiated. Receives the parameters passed to that instance.

How To Use (Raw)

Add uniqueinstanceraw unit to your uses section.

Call the InstanceRunning function: it will return true if there's already a instance running.

There are two variants:

  • Without arguments: will use the executable name as identifier and will not send the command line parameters
  • With two arguments:
    • Identifier: the identifier of the application
    • SendParameters(default = false): send the command line parameters to the already running instance, if any, before quit.

Remarks

  • Tested with win32 (XP SP2) and Linux (Ubuntu 9.04) with fpc 2.4.2.
  • If you put two TUniqueInstance components in the same application with the same identifier your application won't load.
  • The Identifier is optional both to the function and the component. In the case it's not used, the executable name is used as an Identifier. The consequence is that if someone changes the exe name another instance will not be identified. In the other hand, if two different applications use the same Identifier one will prevent the other from launching
    • TIP: to make sure an application will not prevent another from launching you can use a GUID as the Identifier

History

  • 11/01/17 - Version 1.1
    • Make compatible with fpc 3.0 and above
    • Code cleanup
  • 17/04/11 - Version 1.0
    • Fix application being detected as running after a crash under unix
    • Fix crash when compiling with Gtk2 widgetset
    • Set default values of published properties
    • Optimizations and code clean up
    • New icon
  • 02/10/07 - Version 0.2
    • Based in fpc 2.2.0
    • Implemented parameter reception under unix
    • General optimizations and code clean up
  • 16/12/06 - Initial release. See my blog to know how it began.

Author

Luiz Américo Pereira Câmara

License

Modified LGPL


Download

Version 1.1: Download from GitHub

Recent development moved to GitHub repository: https://github.com/blikblum/luipack/tree/master/uniqueinstance

You can use SVN client:

 svn co https://github.com/blikblum/luipack.git/trunk/uniqueinstance

See also