どのようにstart/stopモニタリングイレのでしょうか?
-
20-09-2019 - |
質問
ってみなければならないというニーズモニターのための特定のレジストリの変更をご利用いただけます:standardとexpress。見 液 時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;
私の申請が必要になりモーターのstart/stopこのスレッドの需要が上記のコードを許可しないこと。で設定を終了フラグと思います。
で十分で何らかのスレッドの停止を待って、無料で新規作成します。変更方法を教えてくださいこのコードを達成。
解決
の代わりにWaitForMultipleObjects()
の2つのイベントの配列とWaitForSingleObject()
を使用してください。スレッドクラスに手動リセットイベントを追加し、あなたがTerminated
するTrue
を設定した後にそれを知らせます。合図された二つの事象の戻り値をチェックし、それに応じて行動ます。
の編集の
いくつかの最小限のDelphi 2009のコードは、アイデアを実証します。あなたが使用される単位のリストにSyncObjs
を追加し、追加する必要があります。
fTerminateEvent: TEvent;
あなたのスレッドクラスのprivate
セクションに。
constructor TTestThread.Create;
begin
inherited Create(TRUE);
fTerminateEvent := TEvent.Create(nil, True, False, '');
// ...
Resume;
end;
destructor TTestThread.Destroy;
begin
fTerminateEvent.SetEvent;
Terminate; // not necessary if you don't check Terminated in your code
WaitFor;
fTerminateEvent.Free;
inherited;
end;
procedure TTestThread.Execute;
var
Handles: array[0..1] of THandle;
begin
Handles[0] := ...; // your event handle goes here
Handles[1] := fTerminateEvent.Handle;
while not Terminated do begin
if WaitForMultipleObjects(2, @Handles[0], False, INFINITE) <> WAIT_OBJECT_0 then
break;
// ...
end;
end;
あなたはそれにあなたの質問にコードを追加する必要があります。単純にスレッドを(必要な場合)。
ブロックを解除する必要が全力を尽くしますスレッドのインスタンスを解放しようとしています他のヒント
代わりに無限のすべてWaitForSingleObject時間します。このループを継続し、終了をチェックします。
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;
の方法TThread.利用停止、TThread.履歴書が理論的に使用すると一時停止のスレッドがデ2010年現在認識していない安全で使用するものです。見 TThread.履歴書は推奨されていませんイン-2010年に何をすべき使用するのか。 や http://msdn.microsoft.com/en-us/library/ms686345%28VS.85%29.aspx
これは、あなたが終了呼び出すときに、ちょうど今、以下のような小さな変更を加えると、働くます:
TRegMonitorThread = class(TThread)
...
public
procedure Terminate; reintroduce;
...
procedure TRegMonitorThread. Terminate; // add new public procedure
begin
inherited Terminate;
Windows.SetEvent(FEvent);
end;
procedure TRegMonitorThread.Execute;
begin
InitThread;
while not Terminated do
begin
if WaitForSingleObject(FEvent, INFINITE) = WAIT_OBJECT_0 then
begin
if Terminated then // <- add this 2 lines
Exit;
...
end;
end;
end;