|
| 1 | +unit UniqueInstance; |
| 2 | + |
| 3 | +{ |
| 4 | + UniqueInstance is a component to allow only a instance by program |
| 5 | +
|
| 6 | + Copyright (C) 2006 Luiz Americo Pereira Camara |
| 7 | + |
| 8 | +
|
| 9 | + This library is free software; you can redistribute it and/or modify it |
| 10 | + under the terms of the GNU Library General Public License as published by |
| 11 | + the Free Software Foundation; either version 2 of the License, or (at your |
| 12 | + option) any later version with the following modification: |
| 13 | +
|
| 14 | + As a special exception, the copyright holders of this library give you |
| 15 | + permission to link this library with independent modules to produce an |
| 16 | + executable, regardless of the license terms of these independent modules,and |
| 17 | + to copy and distribute the resulting executable under terms of your choice, |
| 18 | + provided that you also meet, for each linked independent module, the terms |
| 19 | + and conditions of the license of that module. An independent module is a |
| 20 | + module which is not derived from or based on this library. If you modify |
| 21 | + this library, you may extend this exception to your version of the library, |
| 22 | + but you are not obligated to do so. If you do not wish to do so, delete this |
| 23 | + exception statement from your version. |
| 24 | +
|
| 25 | + This program is distributed in the hope that it will be useful, but WITHOUT |
| 26 | + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 27 | + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License |
| 28 | + for more details. |
| 29 | +
|
| 30 | + You should have received a copy of the GNU Library General Public License |
| 31 | + along with this library; if not, write to the Free Software Foundation, |
| 32 | + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +{$mode objfpc}{$H+} |
| 37 | +{$if not defined(Windows) or (FPC_FULLVERSION >= 30001)} |
| 38 | +{$define PollIPCMessage} |
| 39 | +{$endif} |
| 40 | + |
| 41 | +interface |
| 42 | + |
| 43 | +uses |
| 44 | + Forms, Classes, SysUtils, simpleipc, ExtCtrls; |
| 45 | + |
| 46 | +type |
| 47 | + |
| 48 | + TOnOtherInstance = procedure (Sender : TObject; ParamCount: Integer; const Parameters: array of String) of object; |
| 49 | + |
| 50 | + { TUniqueInstance } |
| 51 | + |
| 52 | + TUniqueInstance = class(TComponent) |
| 53 | + private |
| 54 | + FIdentifier: String; |
| 55 | + FOnOtherInstance: TOnOtherInstance; |
| 56 | + FUpdateInterval: Cardinal; |
| 57 | + FEnabled: Boolean; |
| 58 | + FPriorInstanceRunning: Boolean; |
| 59 | + procedure ReceiveMessage(Sender: TObject); |
| 60 | + {$ifdef PollIPCMessage} |
| 61 | + procedure CheckMessage(Sender: TObject); |
| 62 | + {$endif} |
| 63 | + protected |
| 64 | + procedure Loaded; override; |
| 65 | + public |
| 66 | + constructor Create(AOwner: TComponent); override; |
| 67 | + property PriorInstanceRunning: Boolean read FPriorInstanceRunning; |
| 68 | + published |
| 69 | + property Enabled: Boolean read FEnabled write FEnabled default False; |
| 70 | + property Identifier: String read FIdentifier write FIdentifier; |
| 71 | + property UpdateInterval: Cardinal read FUpdateInterval write FUpdateInterval default 1000; |
| 72 | + property OnOtherInstance: TOnOtherInstance read FOnOtherInstance write FOnOtherInstance; |
| 73 | + end; |
| 74 | + |
| 75 | +implementation |
| 76 | + |
| 77 | +uses |
| 78 | + StrUtils, UniqueInstanceBase; |
| 79 | + |
| 80 | +{ TUniqueInstance } |
| 81 | + |
| 82 | +procedure TUniqueInstance.ReceiveMessage(Sender: TObject); |
| 83 | +var |
| 84 | + ParamsArray: array of String; |
| 85 | + Params: String; |
| 86 | + Count, i: Integer; |
| 87 | +begin |
| 88 | + if Assigned(FOnOtherInstance) then |
| 89 | + begin |
| 90 | + //MsgType stores ParamCount |
| 91 | + Count := FIPCServer.MsgType; |
| 92 | + SetLength(ParamsArray, Count); |
| 93 | + Params := FIPCServer.StringMessage; |
| 94 | + for i := 1 to Count do |
| 95 | + ParamsArray[i - 1] := ExtractWord(i, Params, [ParamsSeparator]); |
| 96 | + FOnOtherInstance(Self, Count, ParamsArray); |
| 97 | + end; |
| 98 | +end; |
| 99 | + |
| 100 | +{$ifdef PollIPCMessage} |
| 101 | +procedure TUniqueInstance.CheckMessage(Sender: TObject); |
| 102 | +begin |
| 103 | + FIPCServer.PeekMessage(1, True); |
| 104 | +end; |
| 105 | +{$endif} |
| 106 | + |
| 107 | +procedure TUniqueInstance.Loaded; |
| 108 | +var |
| 109 | + IPCClient: TSimpleIPCClient; |
| 110 | + {$ifdef PollIPCMessage} |
| 111 | + Timer: TTimer; |
| 112 | + {$endif} |
| 113 | +begin |
| 114 | + if not (csDesigning in ComponentState) and FEnabled then |
| 115 | + begin |
| 116 | + IPCClient := TSimpleIPCClient.Create(Self); |
| 117 | + IPCClient.ServerId := GetServerId(FIdentifier); |
| 118 | + if not Assigned(FIPCServer) and IPCClient.ServerRunning then |
| 119 | + begin |
| 120 | + //A older instance is running. |
| 121 | + FPriorInstanceRunning := True; |
| 122 | + //A instance is already running |
| 123 | + //Send a message and then exit |
| 124 | + if Assigned(FOnOtherInstance) then |
| 125 | + begin |
| 126 | + IPCClient.Active := True; |
| 127 | + IPCClient.SendStringMessage(ParamCount, GetFormattedParams); |
| 128 | + end; |
| 129 | + Application.ShowMainForm := False; |
| 130 | + Application.Terminate; |
| 131 | + end |
| 132 | + else |
| 133 | + begin |
| 134 | + if not Assigned(FIPCServer) then |
| 135 | + InitializeUniqueServer(IPCClient.ServerID); |
| 136 | + FIPCServer.OnMessage := @ReceiveMessage; |
| 137 | + //there's no more need for IPCClient |
| 138 | + IPCClient.Destroy; |
| 139 | + {$ifdef PollIPCMessage} |
| 140 | + if Assigned(FOnOtherInstance) then |
| 141 | + begin |
| 142 | + Timer := TTimer.Create(Self); |
| 143 | + Timer.Interval := FUpdateInterval; |
| 144 | + Timer.OnTimer := @CheckMessage; |
| 145 | + end; |
| 146 | + {$endif} |
| 147 | + end; |
| 148 | + end;//if |
| 149 | + inherited; |
| 150 | +end; |
| 151 | + |
| 152 | +constructor TUniqueInstance.Create(AOwner: TComponent); |
| 153 | +begin |
| 154 | + inherited Create(AOwner); |
| 155 | + FUpdateInterval := 1000; |
| 156 | +end; |
| 157 | + |
| 158 | +end. |
| 159 | + |
0 commit comments