How to start/stop a monitoring Delphi thread on demand?

前端 未结 3 1631
有刺的猬
有刺的猬 2021-01-01 08:11

I\'ve been looking for a way to monitor for specific registry changes in Delphi. Found a solution at about.com:

procedure TRegMonitorThread.Execute;
begin
           


        
3条回答
  •  耶瑟儿~
    2021-01-01 08:45

    Instead in INFINITE you should have WaitForSingleObject time out after a period. That way the loop continues and Terminated is checked.

    procedure TRegMonitorThread.Execute;
    begin
      InitThread; // method omitted here
      while not Terminated do
      begin
        if WaitForSingleObject(FEvent, 1000) = WAIT_OBJECT_0 then
        begin
          fChangeData.RootKey := RootKey;
          fChangeData.Key := Key;
          SendMessage(Wnd, WM_REGCHANGE, RootKey, LongInt(PChar(Key)));
          ResetEvent(FEvent);
    
          RegNotifyChangeKeyValue(FReg.CurrentKey, 1, Filter, FEvent, 1);
        end;
      end;
    end;
    

    The methods TThread.Suspend and TThread.Resume could theoretically be used to temporary stop threads, but as Delphi 2010 now acknowledges they are not safe for use. See TThread.resume is deprecated in Delphi-2010 what should be used in place? and http://msdn.microsoft.com/en-us/library/ms686345%28VS.85%29.aspx

提交回复
热议问题