Re: [lazarus] Component to force one instance by application
Al Boldi wrote: Luiz Americo Pereira Camara wrote: Kris Leech wrote: Good stuff, did you come across any issues with simpleIPC, Ive been meaning to have a go with it for a while... I had two issues with simpleipc: - in unix the TSimpleIPCServer.OnMessage event is not fired. To override this, a solution is to call ReadMessage in the OnIdle event of the application. Try this implementation: type { TPeekThread } TPeekThread = Class(TThread) private FDoPeek: TNotifyEvent; protected procedure Execute; override; procedure Synchronize(Method: TThreadMethod); Property DoPeek: TNotifyEvent read FDoPeek write FDoPeek; end; { TSimpleIPCpServer } TSimpleIPCpServer = Class(TSimpleIPCServer) private FPeek: Boolean; FPeekThread: TPeekThread; procedure PeekMethod(Sender: TObject); procedure SetPeek(const AValue: Boolean); protected Procedure Activate; override; Procedure Deactivate; override; published Property Peek : Boolean read FPeek write SetPeek; end; implementation { TPeekThread } procedure TPeekThread.Execute; begin repeat FDoPeek(self); until Terminated; end; procedure TPeekThread.Synchronize(Method: TThreadMethod); begin inherited; end; { TSimpleIPCpServer } procedure TSimpleIPCpServer.PeekMethod(Sender: TObject); begin if PeekMessage(1000,false) then TPeekThread(sender).Synchronize(@ReadMessage); end; procedure TSimpleIPCpServer.SetPeek(const AValue: Boolean); begin if FPeek=AValue then exit; if FActive then DoError(SErrActive,[]); FPeek:=AValue; end; procedure TSimpleIPCpServer.Activate; begin inherited Activate; if FActive and FPeek then begin FPeekThread:=TPeekThread.Create(true); FPeekThread.DoPeek:[EMAIL PROTECTED]; FPeekThread.FreeOnTerminate:=true; FPeekThread.Resume; end; end; procedure TSimpleIPCpServer.Deactivate; begin if FActive and FPeek then begin FPeekThread.Terminate; end; inherited Deactivate; end; Thanks. Another solution is to use the dnotify api of the kernel. Someone already tried dnotify with fpc? BTW: Somebody know how to override a Classname, like it's possible with Methods. i.e: TComponent = Class(TComponent); override; - The other is that there's currently no easy way to diferentiate between different message types. Let's say i need to send messages telling i started the job, send the results of the job and than finished the job. Currently there are two options: send a header in the stream with a numeric variable and check it when arrive (you lose the comodity of SendStringMessage) or you send a string with a string marker. I will propose a change in it (and provide the patch) where we could use the MessageType (user defined) to identify the message and we could still use SendStringMessage . Or you could use another SimpleIPC connection. - another potential problem (not tested) is that in unix a file is used to pass the messages. So if the program crashes the file may not be deleted and would give that the server is running. In unix it uses pipes. No problem when program crashes. Good to know. I don't have experience with pipes. Luiz _ To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe" as the Subject archives at http://www.lazarus.freepascal.org/mailarchives
Re: [lazarus] Component to force one instance by application
Luiz Americo Pereira Camara wrote: > Kris Leech wrote: > > Good stuff, did you come across any issues with simpleIPC, Ive been > > meaning to have a go with it for a while... > > I had two issues with simpleipc: > - in unix the TSimpleIPCServer.OnMessage event is not fired. To > override this, a solution is to call ReadMessage in the OnIdle event of > the application. Try this implementation: type { TPeekThread } TPeekThread = Class(TThread) private FDoPeek: TNotifyEvent; protected procedure Execute; override; procedure Synchronize(Method: TThreadMethod); Property DoPeek: TNotifyEvent read FDoPeek write FDoPeek; end; { TSimpleIPCpServer } TSimpleIPCpServer = Class(TSimpleIPCServer) private FPeek: Boolean; FPeekThread: TPeekThread; procedure PeekMethod(Sender: TObject); procedure SetPeek(const AValue: Boolean); protected Procedure Activate; override; Procedure Deactivate; override; published Property Peek : Boolean read FPeek write SetPeek; end; implementation { TPeekThread } procedure TPeekThread.Execute; begin repeat FDoPeek(self); until Terminated; end; procedure TPeekThread.Synchronize(Method: TThreadMethod); begin inherited; end; { TSimpleIPCpServer } procedure TSimpleIPCpServer.PeekMethod(Sender: TObject); begin if PeekMessage(1000,false) then TPeekThread(sender).Synchronize(@ReadMessage); end; procedure TSimpleIPCpServer.SetPeek(const AValue: Boolean); begin if FPeek=AValue then exit; if FActive then DoError(SErrActive,[]); FPeek:=AValue; end; procedure TSimpleIPCpServer.Activate; begin inherited Activate; if FActive and FPeek then begin FPeekThread:=TPeekThread.Create(true); FPeekThread.DoPeek:[EMAIL PROTECTED]; FPeekThread.FreeOnTerminate:=true; FPeekThread.Resume; end; end; procedure TSimpleIPCpServer.Deactivate; begin if FActive and FPeek then begin FPeekThread.Terminate; end; inherited Deactivate; end; BTW: Somebody know how to override a Classname, like it's possible with Methods. i.e: TComponent = Class(TComponent); override; > - The other is that there's currently no easy way to diferentiate > between different message types. Let's say i need to send messages > telling i started the job, send the results of the job and than finished > the job. Currently there are two options: send a header in the stream > with a numeric variable and check it when arrive (you lose the comodity > of SendStringMessage) or you send a string with a string marker. I will > propose a change in it (and provide the patch) where we could use the > MessageType (user defined) to identify the message and we could still > use SendStringMessage . Or you could use another SimpleIPC connection. > - another potential problem (not tested) is that in unix a file is used > to pass the messages. So if the program crashes the file may not be > deleted and would give that the server is running. In unix it uses pipes. No problem when program crashes. Thanks! -- Al _ To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe" as the Subject archives at http://www.lazarus.freepascal.org/mailarchives
Re: [lazarus] Component to force one instance by application
Kris Leech wrote: Good stuff, did you come across any issues with simpleIPC, Ive been meaning to have a go with it for a while... I had two issues with simpleipc: - in unix the TSimpleIPCServer.OnMessage event is not fired. To override this, a solution is to call ReadMessage in the OnIdle event of the application. - The other is that there's currently no easy way to diferentiate between different message types. Let's say i need to send messages telling i started the job, send the results of the job and than finished the job. Currently there are two options: send a header in the stream with a numeric variable and check it when arrive (you lose the comodity of SendStringMessage) or you send a string with a string marker. I will propose a change in it (and provide the patch) where we could use the MessageType (user defined) to identify the message and we could still use SendStringMessage . - another potential problem (not tested) is that in unix a file is used to pass the messages. So if the program crashes the file may not be deleted and would give that the server is running. Luiz _ To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe" as the Subject archives at http://www.lazarus.freepascal.org/mailarchives
Re: [lazarus] Component to force one instance by application
Good stuff, did you come across any issues with simpleIPC, Ive been meaning to have a go with it for a while... Luiz Americo wrote: I created a component to force one instance per application. More info here: http://wiki.lazarus.freepascal.org/UniqueInstance Luiz _ To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe" as the Subject archives at http://www.lazarus.freepascal.org/mailarchives _ To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe" as the Subject archives at http://www.lazarus.freepascal.org/mailarchives