如何在TService父线程和子线程之间发送和处理消息? [英] How do I send and handle message between TService parent thread and child thread?

查看:198
本文介绍了如何在TService父线程和子线程之间发送和处理消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Delphi 2010来创建一个Windows服务,它将监视多个注册表项,并在发生更改时执行操作。我在delphi.about.com中使用 RegMonitorThread ,而我的问题是我的主服务线程从未收到从TRegMonitorThread发送的消息。

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;

我已经确认该消息正在发送,并在RegMonitorThread中达到了这一点。 pas单元

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;

关于我在这里失踪的任何想法?我会提及它,因为它可能与问题有关,我在Windows 7上。

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.

推荐答案

TServiceThread.Handle是一个线柄,不是窗口把手。您不能使用它来接收Windows消息(它可用于线程管理功能),您必须设置一个窗口句柄。你可以在这里找到一个例子: 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

这篇关于如何在TService父线程和子线程之间发送和处理消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆