本方法参考并使用了CudaText的2个单元(appuniqueinstance.pas和appuniqueinstancebase.pas),appuniqueinstance.pas增加RunOnce。
使用方法:
在项目文件uses添加AppUniqueInstance单元,Application.Initialize下添加runonce(将红色字体的内容添加到你的工程文件[.lpr])就可以实现在windows和linux只运行一个实例。
program project1; {$mode objfpc}{$H+} uses {$IFDEF UNIX} cthreads, {$ENDIF} {$IFDEF HASAMIGA} athreads, {$ENDIF} AppUniqueInstance, Interfaces, // this includes the LCL widgetset Forms, Unit1; {$R *.res} begin
RequireDerivedFormResource:=True; Application.Scaled:=True; Application.Initialize;
RunOnce;
Application.CreateForm(TForm1, Form1); Application.Run;
end.
修改后的AppUniqueInstance,增加RunOnce使用更简单。
unit AppUniqueInstance; { UniqueInstance is a component to allow only a instance by program Copyright (C) 2006 Luiz Americo Pereira Camara pascalive@bol.com.br This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version with the following modification: As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules,and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. } {$mode objfpc}{$H+} {$if not defined(Windows) or (FPC_FULLVERSION >= 30001)} {$define PollIPCMessage} {$endif} interface uses Forms, Classes, SysUtils, simpleipc, ExtCtrls; type TOnOtherInstance = procedure (Sender : TObject; ParamCount: Integer; const Parameters: array of String) of object; { TUniqueInstance } TUniqueInstance = class(TComponent) private FIdentifier: String; FOnOtherInstance: TOnOtherInstance; FUpdateInterval: Cardinal; FEnabled: Boolean; FPriorInstanceRunning: Boolean; {$ifdef PollIPCMessage} Timer: TTimer; {$endif} procedure ReceiveMessage(Sender: TObject); {$ifdef PollIPCMessage} procedure CheckMessage(Sender: TObject); {$endif} public constructor Create(AOwner: TComponent); override; destructor Destroy; override; property PriorInstanceRunning: Boolean read FPriorInstanceRunning; procedure Loaded(const AParams: array of string); reintroduce; //Alexey published property Enabled: Boolean read FEnabled write FEnabled default False; property Identifier: String read FIdentifier write FIdentifier; property UpdateInterval: Cardinal read FUpdateInterval write FUpdateInterval default 1000; property OnOtherInstance: TOnOtherInstance read FOnOtherInstance write FOnOtherInstance; end;
procedure RunOnce; implementation uses StrUtils, AppUniqueInstanceBase; { TUniqueInstance } procedure RunOnce; var AppUniqInst: TUniqueInstance = nil; CmdParams: array of string; begin if not Assigned(AppUniqInst) then AppUniqInst:= TUniqueInstance.Create(nil); if not AppUniqInst.Enabled then begin AppUniqInst.Enabled:= true; AppUniqInst.Loaded(CmdParams); if AppUniqInst.PriorInstanceRunning then begin AppUniqInst.free; Application.Terminate; end; end else begin if Assigned(AppUniqInst) then AppUniqInst.free; end; end; procedure TUniqueInstance.ReceiveMessage(Sender: TObject); var ParamsArray: array of String; Params: String; Count, i: Integer; begin if Assigned(FOnOtherInstance) then begin //MsgType stores ParamCount Count := FIPCServer.MsgType; SetLength(ParamsArray, Count); Params := FIPCServer.StringMessage; for i := 1 to Count do ParamsArray[i - 1] := ExtractWord(i, Params, [ParamsSeparator]); FOnOtherInstance(Self, Count, ParamsArray); end; end; {$ifdef PollIPCMessage} procedure TUniqueInstance.CheckMessage(Sender: TObject); begin if FIPCServer.Active then FIPCServer.PeekMessage(1, True); end; {$endif} procedure TUniqueInstance.Loaded(const AParams: array of string); var IPCClient: TSimpleIPCClient; begin if not (csDesigning in ComponentState) and FEnabled then begin IPCClient := TSimpleIPCClient.Create(Self); IPCClient.ServerId := GetServerId(FIdentifier); if not Assigned(FIPCServer) and IPCClient.ServerRunning then begin //A older instance is running. FPriorInstanceRunning := True; //A instance is already running //Send a message and then exit if Assigned(FOnOtherInstance) then begin IPCClient.Active := True; IPCClient.SendStringMessage(ParamCount, GetFormattedParams(AParams)); end; Application.ShowMainForm := False; Application.Terminate; end else begin if not Assigned(FIPCServer) then InitializeUniqueServer(IPCClient.ServerID); FIPCServer.OnMessage := @ReceiveMessage; //there's no more need for IPCClient IPCClient.Destroy; {$ifdef PollIPCMessage} if Assigned(FOnOtherInstance) and (Timer=nil) then begin Timer := TTimer.Create(Self); Timer.Interval := FUpdateInterval; Timer.OnTimer := @CheckMessage; end; {$endif} end; end;//if inherited Loaded; end; constructor TUniqueInstance.Create(AOwner: TComponent); begin inherited Create(AOwner); FUpdateInterval := 1000; end; destructor TUniqueInstance.Destroy; begin if Assigned(Timer) then begin Timer.Enabled := False; Timer.OnTimer := nil; FreeAndNil(Timer); end; inherited Destroy; end; end.
unit AppUniqueInstanceBase; {$mode objfpc}{$H+} interface uses Classes, SysUtils, simpleipc; const ParamsSeparator = #13; var FIPCServer: TSimpleIPCServer; procedure InitializeUniqueServer(const ServerId: String); function GetFormattedParams(const AParams: array of string): String; //Alexey function GetServerId(const Identifier: String): String; implementation uses LazUTF8; const BaseServerId = 'tuniqueinstance_'; procedure InitializeUniqueServer(const ServerId: String); begin //It's the first instance. Init the server if FIPCServer = nil then begin FIPCServer := TSimpleIPCServer.Create(nil); FIPCServer.ServerID := ServerId; FIPCServer.Global := True; FIPCServer.StartServer; end; end; function GetFormattedParams(const AParams: array of string): String; //Alexey var i: Integer; begin Result := ''; for i := Low(AParams) to High(AParams) do Result := Result + AParams[i] + ParamsSeparator; end; function GetServerId(const Identifier: String): String; begin if Identifier <> '' then Result := BaseServerId + Identifier else Result := BaseServerId + ExtractFileName(ParamStrUTF8(0)); end; finalization FIPCServer.Free; end.