Ideally you should create a background thread and run these tasks from it. If they can run sequentially, it's even better.
You can use our TIWTimedThread class that will handle this nicely:
Code:
uses
IW.Common.Threads;
type
TMyThread = class(TIWTimedThread)
protected
procedure DoExecute; override;
end;
procedure TMyThread.DoExecute;
begin
// do your stuff here
end;
// In your ServerController unit:
// Declare the thread as a field in our ServerController:
type
TIWServerController = class(TIWServerControllerBase)
procedure IWServerControllerBaseNewSession(ASession: TIWApplication);
procedure IWServerControllerBaseConfig(Sender: TObject);
procedure IWServerControllerBaseDestroy(Sender: TObject);
private
FMyThread: TMyThread;
public
end;
// Create and start it from the ServerController.OnConfig event:
procedure TIWServerController.IWServerControllerBaseConfig(Sender: TObject);
begin
FMyThread := TMyThread.Create('My background thread', Interval_in_miliseconds);
FMyThread.Start;
end;
// Finalize the thread in ServerController's OnDestroy event:
procedure TIWServerController.IWServerControllerBaseDestroy(Sender: TObject);
begin
if Assigned(FMyThread) then // always check because creation of ServerController may fail if something is wrong
FMyThread.Finish; // Finish will terminate and destroy the thread
end;
TIWTimedThread will call the DoExecute method at regular intervals, which is specified when you create the thread.
I recommend you to put the code of the thread in a separate unit. Also, when working with threads: If you need any database connectivity from within the thread, create an exclusive connection in the thread itself, don't try to "share" an external connection.
I'll see if I can publish a demo showing an usage example like this.
Cheers