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?
Use
WaitForMultipleObjects()
with an array of two events instead ofWaitForSingleObject()
. Add a manual reset event to the thread class, and signal it after you have setTerminated
toTrue
. Check the return value which of the two events has been signalled, and act accordingly.Edit:
Some minimal Delphi 2009 code to demonstrate the idea. You have to add
SyncObjs
to the list of used units, and addto the
private
section of your thread class.You only need to add the code in your question to it. Simply trying to free the thread instance will do everything necessary to unblock the thread (if necessary).