You are not logged in.
Pages: 1
Hello,
Within my server I have to launch one custom TThread for doing some non-critical action in background. Its execution is generally around 2 min, then stop and call DoTerminate to notify server he can do some other action. In a console app, everything is working fine with the SynCommons.ConsoleWaitForEnterKey procedure which call CheckSynchronize. But when i'm using my server as Windows service, with TServiceSingle, there is no CheckSynchronize call.
What would be the best way to implement a similar logic to call CheckSynchronize in services please?
Offline
Thanks for reply.
Maybe there is something I do not understand (it is very likely, i'm not used to services), but my service isn't waiting while my secondary thread is running, I just want to execute one method when thread is done.
My purpose is to execute some long procedure in background, and when it's finished notify users by emails, but I can't send the email from my background task for various reason. Maybe I could achieve this with TSynBackgroundThread* classes? Haven't looked at it yet, as my threaded class is already tested.
Offline
Right now, simply with the OnTerminate event of the thread. They do not talk to each other while thread execution. I just need to briefly reenter in the main thread when execution is over.
Offline
Do you need to pass some information to the mainThread or just a notification that job is finished? If you only need notification you can use Events for that.
If you need to pass some information as well, perhaps PostThreadMessage would work (it's not clear if it works in services) or you can use named pipes, memory mapped files or sockets.
Offline
The is no such thing as a "main thread" in a Windows service, I'm afraid, since there is no GUI thread.
So PostThreadMessage won't help.
You can use named pipes or socket.
But calling one thread method from another is the way to go, in your case, I'm quite sure.
Offline
Maybe that help: https://github.com/gabr42/OmniThreadLibrary
Offline
http://stackoverflow.com/questions/2245 … ws-service
OmniThreadLibrary in a service works just fine. There's an example in the repo showing that.
Last edited by George (2017-03-20 11:33:54)
Offline
Thanks all for reply and trying to help !
@igors233: I needed to pass some information contained within my thread yes.
Yes indeed, I wanted to avoid using OTL as what I wanted to do was quite "simple" (and a lot of work with OTL just for this).
So what i've done in the end is override my DoTerminate method from my TThread like that:
procedure TMyThread.DoTerminate;
begin
if fByPassSynchronize then
begin
if Assigned(OnTerminate) then OnTerminate(Self);
end else
inherited;
end;
So when i'm within a service I juste have to set BypassSynchronize := true and my OnTerminate event is well fired at the end.
I've just added a locker to protect shared ressource in the onterminate method just in case,
I will run some more tests as I didn't had time today, but I think this will be enough for my purpose. If not I will make a server method and initiate a client to call it.
Thanks for taking the time even if this isn't directly related to mORMot ; )
Offline
Pages: 1