#1 2020-12-10 14:47:41

leus
Member
Registered: 2012-09-05
Posts: 79

TimerEnable example?

Hello,

Can anybody please share an example on how to use TimerEnable / TimerDisable? I cannot find any example or documentation about it. The commentary on the source code says the following:

Define a task running on a periodic number of seconds in a background thread

  • could be used to run background maintenance or monitoring tasks on this TSQLRest instance, at a low pace (typically every few minutes)

  • will instantiate and run a shared TSynBackgroundTimer instance for this TSQLRest, so all tasks will share the very same thread

  • you can run BackgroundTimer.EnQueue or ExecuteNow methods to implement a FIFO queue, or force immediate execution of the process

  • will call BeginCurrentThread/EndCurrentThread as expected e.g. by logs

But no mention on how to actually use it. Any example on how to use it?

Last edited by leus (2020-12-10 14:51:18)

Offline

#2 2020-12-10 14:59:12

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,603
Website

Re: TimerEnable example?

Just call TimerEnable() with the proper callback which will be raised at the given pace.

Offline

#3 2020-12-10 15:06:09

leus
Member
Registered: 2012-09-05
Posts: 79

Re: TimerEnable example?

That's the question.

Offline

#4 2020-12-10 16:02:47

tbo
Member
Registered: 2015-04-20
Posts: 353

Re: TimerEnable example?

I use it in a small application as follows:

procedure TDWSRestServer.DownloadDWData(pmSender: TSynBackgroundTimer; pmEvent: TWaitResult; const pmcMsg: RawUTF8);
const
  URL_TEMPLATE = 'https://...';
var
  log: ISynLog;
  url: SockString;
  content: SockString;
begin
  log := LogClass.Enter(Self, 'DownloadDWData');
  with FRestServiceSettings do
    url := StringToAnsi7(FormatString(URL_TEMPLATE, [DWUserName, DWUserPassword, DWUserAPIToken]));

  content := HttpGet(url);
  ...
end;

procedure TDWSRestServer.Start;
var
  timer: TSynBackgroundTimer;
begin
  timer := TimerEnable(DownloadDWData, FRestServiceSettings.DataUpdateInterval);
  if Assigned(timer) then
    timer.ExecuteNow(DownloadDWData);
end;

procedure TDWSRestServer.Stop;
begin
  TimerDisable(DownloadDWData);
end;

With best regards
Thomas

Offline

#5 2020-12-10 16:50:13

leus
Member
Registered: 2012-09-05
Posts: 79

Re: TimerEnable example?

I did this, because my task lives inside my service (not in the server). Is this right?

someService := T.Create();
aRestServer.ServiceDefine(someService, [ISomeService]);
aRestServer.TimerEnable(someService.MyBackgroundTask, 360);

Last edited by leus (2020-12-10 16:50:35)

Offline

#6 2020-12-10 18:42:07

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,603
Website

Re: TimerEnable example?

Sounds just fine.

Offline

#7 2024-06-19 01:52:17

zen010101
Member
Registered: 2024-06-15
Posts: 48

Re: TimerEnable example?

I have some inquiries regarding this previous thread:

Does a TSynBackgroundTimer utilize a thread to sequentially execute aOnProcess: TOnSynBackgroundTimerProcess?

How can I employ separate threads to execute multiple tasks concurrently in the background? Is it possible to use multiple TSynBackgroundTimer instances for this purpose? 

Would you please provide me with more examples?

Offline

#8 2024-06-19 07:36:51

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,603
Website

Re: TimerEnable example?

Each TSynBackgroundTimer maintain its own thread, in which you can run events.

Offline

Board footer

Powered by FluxBB