En/na Mattias Gärtner ha escrit:
Is there already a cross platform component for applications to check if it is
already running and if yes then pass some commands to the already application?
For example clicking on files in the file browser should not open a second
instance, but tell the already running instance to open the files.
There's uniqueinstance
http://sourceforge.net/project/shownotes.php?group_id=92177&release_id=471823
(The wiki is down, so I cannot give you the wiki link) *but* it doesn't
work here under Linux (I could launch more that one instance of my program).
Since I hadn't too much time to debug it, I slightly modified it to open
and lock a file instead of using TSimpleIpc, but that makes it
non-crossplatform.
Since it's short I'm including it here, it could be extended to write
the pid in the file, so another instance can somewhat contact the first one.
Bye
--
Luca
unit uniqueinstanceraw;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,baseunix,unix;
function InstanceRunning(const Identifier: String): Boolean;
function InstanceRunning: Boolean;
implementation
const
BaseServerId = 'tuniqueinstance_';
function InstanceRunning(const Identifier: String): Boolean;
var
FLockFile:cint;
function GetServerId: String;
begin
if Identifier <> '' then
Result:=BaseServerId+Identifier
else
Result:=BaseServerId+ExtractFileName(ParamStr(0));
end;
begin
//cuando el programa termine el lock desaparece solo
result:=true;
FLockFile:=fpOpen('/tmp/'+GetServerId, O_RDWR or O_CREAT);
if FLockFile>0 then
result:=fpFlock(FLockFile, LOCK_EX or LOCK_NB)<>0;
end;
function InstanceRunning: Boolean;
begin
InstanceRunning('');
end;
end.