I want to make service application in Delphi that run and copy some files everyday at 02:00 PM. So i have used timer. but control not going to timer event and Service termin
Instead of a timer, use a simple thread launched in OnStart event.
A tutorial is here:
http://www.tolderlund.eu/delphi/service/service.htm
TTimer is better suited for GUI applications. They need a message pump (see here):
TTimer requires a running message queue in order to receive the WM_TIMER message that allows the OS to pass the message to the HWND, or to trigger the specified callback
As others have explained, you cannot simply use a TTimer
component inside a Windows Service Application, as it relies on a message pump which does not come by default in a Service. I see four main options:
TTimer
, I would recommend #2 above, and here's why.
#1 might be a bit much for your scenario, I'm sure you don't want to go that far.
#3 might be easier, but the service's thread needs a little special treatment which I'm also sure you don't need to care about.
#4 might be the ideal solution, but I won't try to change your decision on a service.
Creating a thread is the way to go because it's rather simple and expandable. All my service applications work on a multi-threaded basis, and nothing ever goes inside the actual service's thread, other than handling the actual service.
I was working on a sample for you, but I over-complicated it and it would be a lot of pollution to include it here. I hope at least I got you moving in the right direction.
When you say "service terminates after 15 seconds" it makes me think you are debugging the code.
If you don't have any option and can't use what others suggested, with the code above the timer event is triggered properly when you install and start the service via services.msc. However, if you are debugging the service, the timer event will not be triggered and the aplication will terminate (as you said). I would create a procedure to be called inside timer event, and call it once in ServiceExecute event, so you could debug like this:
procedure TSomeService.ServiceExecute(Sender: TService);
begin
ExecuteSomeProcess(); // Put breakpoint here to debug
while not self.Terminated do
ServiceThread.ProcessRequests(true);
end;
procedure TSomeService.TimerTimer(Sender: TObject);
begin
timer.Enabled := false;
ExecuteSomeProcess(); // This can't throw any exception!
timer.Enabled := true;
end;