如何在主进程内向线程发消息,线程里又如何接收???
恳请给个简单的例子。

解决方案 »

  1.   

    对于发消息,你只需要一个可以接收的句柄即可,
    然后用sendmessage(handle,...)或postmessage()接收消息简单的你可以直接重载WndPro函数
    procedure WndProc(var Message: TMessage); override;对于线程里也是一样的 ,你可以用主窗体来接收消息
      

  2.   

    SendMessage(线程对象.Handle, .....);
      

  3.   

    给你个例子参考了:
    线程单元:
    unit Unit2;interfaceuses
      Windows, Messages, Classes;const
      //自军定义消息;
      MSG_MYMSG = WM_USER + 100;type
      TMyThread = class(TThread)
      private
        FMainHandle: THandle;       //调用本线程的主线程句柄;
      protected
        procedure Execute; override;
      public
        constructor Create(AHandle: THandle);
      end;implementation{ TMyThread }constructor TMyThread.Create(AHandle: THandle);
    begin
      FMainHandle := AHandle;
      inherited Create(True);
    end;procedure TMyThread.Execute;
    begin
      { Place thread code here }
      FreeOnTerminate := True;
      while not Terminated do
      begin
        //这里放你的处理;
        if FMainHandle <> 0 then  //反馈进度;
          PostMessage(FMainHandle, MSG_MYMSG, 0, 0);
      end;
    end;end.窗体单元:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Unit2;type
      TForm1 = class(TForm)
      private
      protected
        procedure WndProc(var Message: TMessage); override;
      public
      end;var
      Form1: TForm1;implementation{$R *.DFM}{ TForm1 }{ TForm1 }procedure TForm1.WndProc(var Message: TMessage);
    begin
      if Message.Msg = MSG_MYMSG then
      begin
        //在这里处理消息;
      end
      else inherited;
    end;end.
      

  4.   

    PostThreadMessage:用于向一个线程的消息队列发送一个消息。
      idThread: DWORD;  目标线程的ID;
      Msg: UINT;        要发送的消息的数值;(一般是以WM_开头的常量)
      wParam: WPARAM;   第一个消息参数(其值和具体的消息有关);
      lParam: LPARAM    第二个消息参数(其值和具体的消息有关);例子:在SvcMgr.pas单元中:
    procedure TService.Controller(CtrlCode: DWord);
    begin
      PostThreadMessage(ServiceThread.ThreadID, CM_SERVICE_CONTROL_CODE, CtrlCode, 0);
      if ServiceThread.Suspended then ServiceThread.Resume;
    end;PeekMessage:用于从消息队列中获取一个消息,一般用于程序的主消息循环;
      lpMsg: TMsg;       指向消息结构的指针;
      hWnd: HWND;        窗口句柄,如果指定,则只接收指定窗口的消息;
      wMsgFilterMin,wMsgFilterMax: UINT    指定接收消息范围,如指定则只接收指定范围内的消息;
      wRemoveMsg: UINT   消息处理标志,指定消息处理后是否从队列中移去。例子:Forms.pas单元:
    function TApplication.ProcessMessage(var Msg: TMsg): Boolean;
    var
      Handled: Boolean;
    begin
      Result := False;
      if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
      begin
        Result := True;
        if Msg.Message <> WM_QUIT then
        begin
          Handled := False;
          if Assigned(FOnMessage) then FOnMessage(Msg, Handled);
          if not IsHintMsg(Msg) and not Handled and not IsMDIMsg(Msg) and
            not IsKeyMsg(Msg) and not IsDlgMsg(Msg) then
          begin
            TranslateMessage(Msg);
            DispatchMessage(Msg);
          end;
        end
        else
          FTerminate := True;
      end;
    end;注:用PostThreadMessage发送的消息只能用GetMessage才可以接收到。
      

  5.   

    也可参考这里:http://www.01cn.net/cgi-bin/topic_show.cgi?id=115&h=1&bpg=1&age=0