delphi service application stop after 15 second, timer not executing

前端 未结 3 1335
梦如初夏
梦如初夏 2021-01-16 08:35

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

相关标签:
3条回答
  • 2021-01-16 09:22

    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

    0 讨论(0)
  • 2021-01-16 09:27

    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:

    1. Implement a message pump to be able to use the TTimer,
    2. Use a Thread to continuously check for the date/time
    3. Just like #2, use the Service's OnExecute event to check for date/time
    4. Utilize Windows' Scheduled Tasks

    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.

    0 讨论(0)
  • 2021-01-16 09:32

    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;
    
    0 讨论(0)
提交回复
热议问题