如何在TService父线程和子线程之间发送和处理消息?
我正在使用 Delphi 2010 创建一个 Windows 服务,该服务将监视多个注册表项,并在发生更改时执行操作。我正在使用来自 delphi.about.com 的 RegMonitorThread ,我的问题是我的主服务线程永远不会接收从 TRegMonitorThread 发送的消息。
type
TMyService = class(TService)
procedure ServiceExecute(Sender: TService);
procedure ServiceShutdown(Sender: TService);
procedure ServiceStart(Sender: TService; var Started: Boolean);
private
function main: boolean;
{ Private declarations }
public
function GetServiceController: TServiceController; override;
procedure WMREGCHANGE(var Msg: TMessage); message WM_REGCHANGE;
{ Public declarations }
end;
--
procedure TMyService.ServiceStart(Sender: TService; var Started: Boolean);
begin
with TRegMonitorThread.Create do
begin
FreeOnTerminate := True;
Wnd := ServiceThread.Handle;
Key := 'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters';
RootKey := HKEY_LOCAL_MACHINE;
WatchSub := True;
Start;
end;
end;
这是我尝试处理从注册表通知线程发送的消息的地方...但这似乎从未被调用。
procedure TMyService.WMREGCHANGE(var Msg: TMessage);
begin
OutputDebugString(PChar('Registry change at ' + DateTimeToStr(Now)));
end;
我已经确认消息正在发送,并且正在到达 RegMonitorThread.pas 单元中的代码点。
procedure TRegMonitorThread.Execute;
begin
InitThread;
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;
关于我在这里缺少的内容有什么想法吗?我会提到它,因为它可能与问题相关,我使用的是 Windows 7。
I am using Delphi 2010 to create a Windows service that will monitor several registry keys, and perform an action when a change occurs. I am using RegMonitorThread from delphi.about.com, and my issue is that my main service thread never receives the message that is sent from the TRegMonitorThread.
type
TMyService = class(TService)
procedure ServiceExecute(Sender: TService);
procedure ServiceShutdown(Sender: TService);
procedure ServiceStart(Sender: TService; var Started: Boolean);
private
function main: boolean;
{ Private declarations }
public
function GetServiceController: TServiceController; override;
procedure WMREGCHANGE(var Msg: TMessage); message WM_REGCHANGE;
{ Public declarations }
end;
--
procedure TMyService.ServiceStart(Sender: TService; var Started: Boolean);
begin
with TRegMonitorThread.Create do
begin
FreeOnTerminate := True;
Wnd := ServiceThread.Handle;
Key := 'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters';
RootKey := HKEY_LOCAL_MACHINE;
WatchSub := True;
Start;
end;
end;
Here is where I attempt to handle the message sent from the registry notification thread...but this never seems to be called.
procedure TMyService.WMREGCHANGE(var Msg: TMessage);
begin
OutputDebugString(PChar('Registry change at ' + DateTimeToStr(Now)));
end;
I have confirmed that the message is being sent, and is reaching this point of code in the RegMonitorThread.pas unit
procedure TRegMonitorThread.Execute;
begin
InitThread;
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;
Any ideas on what I'm missing here? I'll mention it because it may be relevant to the problem, I am on Windows 7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
TServiceThread.Handle 是线程句柄,而不是窗口句柄。你不能用它来接收窗口消息(它可以在线程管理函数中使用),你必须设置一个窗口句柄。您可以在此处找到示例: http://delphi.about.com/od/ windowsshellapi/l/aa093003a.htm
TServiceThread.Handle is a thread handle, not a window handle. You can't use it to receive windows messages (it is available to be used in thread management functions), you have to setup a window handle. You can find an example here: http://delphi.about.com/od/windowsshellapi/l/aa093003a.htm
我经常遇到同样的问题。我查看了 OmniThreadLibrary,它对于我的目的来说似乎有点过分了。我编写了一个简单的库,称为 TCommThread。它允许您将数据传递回主线程,而不必担心线程或 Windows 消息的任何复杂性。
如果您想尝试的话,这是代码。
CommThread 库:
要使用该库,只需从 TCommThread 线程派生您的线程并覆盖执行过程:
接下来,创建 TStatusCommThreadDispatch 组件的后代并设置其事件。
确保将 CommThreadClass 设置为 TCommThread 后代。
现在您需要做的就是通过 MyCommThreadComponent 创建线程:
添加任意数量的参数和对象。在线程的 Execute 方法中,您可以检索参数和对象。
参数将自动释放。您需要自己管理对象。
要从线程执行方法将消息发送回主线程:
同样,参数将自动销毁,您必须自己管理对象。
要在主线程中接收消息,请附加 OnReceiveThreadMessage 事件或重写 DoOnReceiveThreadMessage 过程:
使用重写的过程来处理发送回主线程的消息:
消息在 ProcessMessageQueue 过程中进行泵送。该过程通过 TTimer 调用。如果您在控制台应用程序中使用该组件,则需要手动调用ProcessMessageQueue。计时器将在第一个线程创建时启动。当最后一个线程完成时它将停止。如果您需要控制计时器何时停止,您可以覆盖已完成过程。您还可以通过重写 DoOnStateChange 过程来根据线程的状态执行操作。
看一下 TCommThread 后代 TStatusCommThreadDispatch。它实现了将简单的状态和进度消息发送回主线程。
我希望这对您有所帮助,并且我已经解释得很好。
I have often run into the same problem. I took a look at OmniThreadLibrary and it looked like overkill for my purposes. I wrote a simple library I call TCommThread. It allows you to pass data back to the main thread without worrying about any of the complexities of threads or Windows messages.
Here's the code if you'd like to try it.
CommThread Library:
To use the library, simply descend your thread from the TCommThread thread and override the Execute procedure:
Next, create a descendant of the TStatusCommThreadDispatch component and set it's events.
Make sure you set the CommThreadClass to your TCommThread descendant.
Now all you need to do is create the threads via MyCommThreadComponent:
Add as many parameters and objects as you like. In your threads Execute method you can retrieve the parameters and objects.
Parameters will be automatically freed. You need to manage objects yourself.
To send a message back to the main thread from the threads execute method:
Again, parameters will be destroyed automatically, objects you have to manage yourself.
To receive messages in the main thread either attach the OnReceiveThreadMessage event or override the DoOnReceiveThreadMessage procedure:
Use the overridden procedure to process the messages sent back to your main thread:
The messages are pumped in the ProcessMessageQueue procedure. This procedure is called via a TTimer. If you use the component in a console app you will need to call ProcessMessageQueue manually. The timer will start when the first thread is created. It will stop when the last thread has finished. If you need to control when the timer stops you can override the Finished procedure. You can also perform actions depending on the state of the threads by overriding the DoOnStateChange procedure.
Take a look at the TCommThread descendant TStatusCommThreadDispatch. It implements the sending of simple Status and Progress messages back to the main thread.
I hope this helps and that I've explained it OK.
嗯,我不知道 ServiceThread.Handle 及其在 Windows 7 上的行为方式,但更安全的方法可能是通过“AllocateHwnd”创建一个新的窗口句柄。然后只需使用 WndProc 即可。像这样的东西(顺便问一下,您是否检查了窗口的句柄是否是有效值?):
像这样释放它
WndProc 过程
这适用于 Windows 7 的线程和服务。我在几个地方使用它。它认为使用一些内部VCL服务窗口更安全。
Hm I don't know about ServiceThread.Handle and how it behaves on Windows 7, but a safer way would probably be to just create a new window handle via "AllocateHwnd". Then just use a WndProc for it. Something like this (by the way did you check that the handle to the windows is a valid value?):
Deallocate it like this
The WndProc procedure
This works on Windows 7 in threads and services. I use it in couple of places. It think it is safer to use that some internal VCL service windows.
这和我之前的回答有关,但我限制在30000个字符。
以下是使用 TCommThread 的测试应用程序的代码:
测试应用程序 (.pas)
测试应用程序 (.dfm)
This is related to my previous answer, but I was limited to 30000 characters.
Here's the code for a test app that uses TCommThread:
Test App (.pas)
Test app (.dfm)