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
InitThread; // method omitted here
while not Terminated do
begin
if WaitForSingleObject(FEvent, INFINITE) = 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;
In my application I will need to start and stop this thread on demand, but the above code does not permit that. Just setting the Terminated flag won't do.
It would be sufficient to somehow tell the thread to stop waiting, then free it and create a new one when needed. How can I change this code to achieve that?
Terminated
. That property isn't a thread-friendly mechanism. I use a termination event exclusively, and then simply provide a new method for other threads to call if they want to stop the thread. – Moseley