How to implement thread which periodically checks something using minimal resources?

前端 未结 5 1630
长发绾君心
长发绾君心 2021-01-02 00:39

I would like to have a thread running in background which will check connection to some server with given time interval. For example for every 5 seconds.

I don\'t kn

5条回答
  •  长发绾君心
    2021-01-02 01:03

    In OmniThreadLibrary, you would do:

    uses
      OtlTask,
      OtlTaskControl;
    
    type
      TTimedTask = class(TOmniWorker)
      public
        procedure Timer1;
      end;
    
    var
      FTask: IOmniTaskControl;
    
    procedure StartTaskClick;
    begin
      FTask := CreateTask(TTimedTask.Create())
        .SetTimer(1, 5*1000, @TTimedTask.Timer1)
        .Run;
    end;
    
    procedure StopTaskClick;
    begin
      FTask.Terminate;
      FTask := nil;
    end;
    
    procedure TTimedTask.Timer1;
    begin
      // this is triggered every 5 seconds
    end;
    

    As for sleeping in Execute - it depends on how you do it. If you use Sleep, then this might not be very wise (for example because it would prevent the thread to stop during the sleep). Sleeping with WaitForSingleObject is fine.

    An example of TThread and WaitForSingleObject:

    type
      TTimedThread = class(TThread)
      public
        procedure Execute; override;
      end;
    
    var
      FStopThread: THandle;
      FThread: TTimedThread;
    
    procedure StartTaskClick(Sender: TObject);
    begin
      FStopThread := CreateEvent(nil, false, false, nil);
      FThread := TTimedThread.Create;
    end;
    
    procedure StopTaskClick(Sender: TObject);
    begin
      SetEvent(FStopThread);
      FThread.Terminate;
      FThread.Free;
      CloseHandle(FStopThread);
    end;
    
    { TTimedThread }
    
    procedure TTimedThread.Execute;
    begin
      while WaitForSingleObject(Form71.FStopThread, 5*1000) = WAIT_TIMEOUT do begin
        // this is triggered every 5 seconds
      end;
    end;
    

    OTL timer implementation is similar to the TThread code above. OTL timers are kept in priority list (basically the timers are sorted on the "next occurence" time) and internal MsgWaitForMultipleObjects dispatcher in TOmniWorker specifies the appropriate timeout value for the highest-priority timer.

提交回复
热议问题