singleinstance.pp 3.5 KB

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