| 1 |
unit singleinstance; |
| 2 |
|
| 3 |
{ |
| 4 |
This file is part of the Free Component Library (FCL) |
| 5 |
Copyright (c) 2015 by Ondrej Pokorny |
| 6 |
|
| 7 |
See the file COPYING.FPC, included in this distribution, |
| 8 |
for details about the copyright. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
**********************************************************************} |
| 15 |
|
| 16 |
|
| 17 |
{$H+} |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses |
| 22 |
SysUtils, Classes; |
| 23 |
|
| 24 |
type |
| 25 |
|
| 26 |
TBaseSingleInstance = class; |
| 27 |
|
| 28 |
//siServer: No other instance is running. The server is started. |
| 29 |
//siClient: There is another instance running. This instance is used as client. |
| 30 |
//siNotResponding: There is another instance running but it doesn't respond. |
| 31 |
TSingleInstanceStart = (siServer, siClient, siNotResponding); |
| 32 |
TSingleInstanceParamsEvent = procedure(Sender: TBaseSingleInstance; Params: TStringList) of object; |
| 33 |
TBaseSingleInstance = class(TComponent) |
| 34 |
private |
| 35 |
FStartResult: TSingleInstanceStart; |
| 36 |
FTimeOutMessages: Integer; |
| 37 |
FTimeOutWaitForInstances: Integer; |
| 38 |
FOnServerReceivedParams: TSingleInstanceParamsEvent; |
| 39 |
Protected |
| 40 |
function GetIsClient: Boolean; virtual; abstract; |
| 41 |
function GetIsServer: Boolean; virtual; abstract; |
| 42 |
function GetStartResult: TSingleInstanceStart; virtual; |
| 43 |
procedure DoServerReceivedParams(const aParamsDelimitedText: string); |
| 44 |
Procedure SetStartResult(AValue : TSingleInstanceStart); |
| 45 |
public |
| 46 |
constructor Create(aOwner: TComponent); override; |
| 47 |
destructor Destroy; override; |
| 48 |
public |
| 49 |
//call Start when you want to start single instance checking |
| 50 |
function Start: TSingleInstanceStart; virtual; abstract; |
| 51 |
//stop single instance server or client |
| 52 |
procedure Stop; virtual; abstract; |
| 53 |
|
| 54 |
//check and handle pending messages on server |
| 55 |
procedure ServerCheckMessages; virtual; abstract; |
| 56 |
//post cmd parameters from client to server |
| 57 |
procedure ClientPostParams; virtual; abstract; |
| 58 |
public |
| 59 |
property TimeOutMessages: Integer read FTimeOutMessages write FTimeOutMessages; |
| 60 |
property TimeOutWaitForInstances: Integer read FTimeOutWaitForInstances write FTimeOutWaitForInstances; |
| 61 |
property OnServerReceivedParams: TSingleInstanceParamsEvent read FOnServerReceivedParams write FOnServerReceivedParams; |
| 62 |
public |
| 63 |
property StartResult: TSingleInstanceStart read GetStartResult; |
| 64 |
property IsServer: Boolean read GetIsServer; |
| 65 |
property IsClient: Boolean read GetIsClient; |
| 66 |
end; |
| 67 |
TBaseSingleInstanceClass = class of TBaseSingleInstance; |
| 68 |
|
| 69 |
ESingleInstance = class(Exception); |
| 70 |
|
| 71 |
Var |
| 72 |
DefaultSingleInstanceClass : TBaseSingleInstanceClass = Nil; |
| 73 |
|
| 74 |
implementation |
| 75 |
|
| 76 |
{ TBaseSingleInstance } |
| 77 |
|
| 78 |
constructor TBaseSingleInstance.Create(aOwner: TComponent); |
| 79 |
begin |
| 80 |
inherited Create(aOwner); |
| 81 |
|
| 82 |
FTimeOutMessages := 1000; |
| 83 |
FTimeOutWaitForInstances := 100; |
| 84 |
end; |
| 85 |
|
| 86 |
destructor TBaseSingleInstance.Destroy; |
| 87 |
begin |
| 88 |
Stop; |
| 89 |
|
| 90 |
inherited Destroy; |
| 91 |
end; |
| 92 |
|
| 93 |
procedure TBaseSingleInstance.DoServerReceivedParams( |
| 94 |
const aParamsDelimitedText: string); |
| 95 |
var |
| 96 |
xSL: TStringList; |
| 97 |
begin |
| 98 |
if not Assigned(FOnServerReceivedParams) then |
| 99 |
Exit; |
| 100 |
|
| 101 |
xSL := TStringList.Create; |
| 102 |
try |
| 103 |
xSL.DelimitedText := aParamsDelimitedText; |
| 104 |
FOnServerReceivedParams(Self, xSL); |
| 105 |
finally |
| 106 |
xSL.Free; |
| 107 |
end; |
| 108 |
end; |
| 109 |
|
| 110 |
function TBaseSingleInstance.GetStartResult: TSingleInstanceStart; |
| 111 |
begin |
| 112 |
Result := FStartResult; |
| 113 |
end; |
| 114 |
|
| 115 |
Procedure TBaseSingleInstance.SetStartResult(AValue : TSingleInstanceStart); |
| 116 |
|
| 117 |
begin |
| 118 |
FStartResult:=AValue; |
| 119 |
end; |
| 120 |
|
| 121 |
end. |
| 122 |
|