How to execute a task at a given time each day

In my THttpSysServer server implementation I need to run maintenance tasks at a certain time each day.
How can I do this with Sparkle, since creating a Timer in the StartServer routine doesn't seem like a good idea to me.

Why not? The bigger question is more about what your maintenance task is doing and whether it can or should be launched in its own thread, perhaps?

I run a Omni Thread Library Background Worker in my server apps and then either push jobs to it using a timer or the OTL Timer thread.

Finally this seems to work for me
InStartServer I have:

  mTmrId: Integer;

  mTmrId := SetTimer(0, 1, 60000, @TmrProc);

And the timer event is:

procedure TmrProc(hWindow : HWND; uMsg : cardinal; idEvent : cardinal; dwTime : DWORD); stdcall;

procedure TmrProc(hWindow : HWND; uMsg : cardinal; idEvent : cardinal;dwTime : DWORD); stdcall;
  KillTimer(0, mTmrId);

  // Check the time and do maintenance tasks here....................
  mTmrId := SetTimer(0, 1, 60000, @TmrProc);

It's important to note that such task has virtually nothing to do with the HTTP server mechanism Sparkle provides. One communicates what clients. The other is a background job. It's often confused because they are both "servers" but they do not relate.

Having said that, Sparkle provides, for convenience, a class TJobRunner (in unit Sparkle.Sys.JobRunner) to help users that want to run periodic tasks in a Delphi server application (it can be used in console or service application, where TTImer sometimes doesn't work very well).

1 Like