Difference between revisions of "UniqueInstance"

From Lazarus wiki
Jump to navigationJump to search
Line 22: Line 22:
 
=== 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 ===

Revision as of 04:33, 3 October 2007

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

About

UniqueInstance provides an easy way to force only one instance per application running at same time.

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

Properties:

  • Enabled: enables/disables the component
  • Identifier: used to provides a way to identify the application

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 Feisty).
  • 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). Not more true since version 0.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.
  • To make sure a application will not avoid other launching you can use a GUID as the Identifier

History

  • 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

Lazarus CCR sourceforge.