定义一个线程类
  //用线程来检测系统配置
  TSysDetectThd = Class (TThread)
  private
    FForm: TForm;
  protected
    Procedure Execute; Override;
  public
    constructor Create(AForm: TForm);
  end;在 procedure TSysDetectThd.Execute; 事件中向另一个窗体FrmSysDetect 发自定义的消息
procedure TSysDetectThd.Execute;
begin
  inherited;
  FreeOnTerminate := True;  TFrmSysDetect(FForm).IsSysDetecting := True;  if not Assigned(FForm) then Exit;
  if not Func_GetVersionDirectX() then
  begin
    if not Assigned(FForm) then Exit;
    PostMessage(FForm.Handle, WM_ShowSysDetect, 0, 0);
  end
  else
  begin
    if not Assigned(FForm) then Exit;
    PostMessage(FForm.Handle, WM_ShowSysDetect, 0, 1);
  end;
  showmessage('one');
  Sleep(500);
  if not Assigned(FForm) then Exit;
  if not Func_GetVersionMedia() then begin
    if not Assigned(FForm) then Exit;
    PostMessage(FForm.Handle, WM_ShowSysDetect, 1, 0);
  end
  else begin
    if not Assigned(FForm) then Exit;
    PostMessage(FForm.Handle, WM_ShowSysDetect, 1, 1);
  end;
  showmessage('two');
  Sleep(500);
  if not Assigned(FForm) then Exit;
  if Func_IsHaveSoundCard() < 1 then begin
    if not Assigned(FForm) then Exit;
    PostMessage(FForm.Handle, WM_ShowSysDetect, 2, 0)
  end
  else begin
    if not Assigned(FForm) then Exit;
    PostMessage(FForm.Handle, WM_ShowSysDetect, 2, 1);
  end;
  showmessage('three');
  TFrmSysDetect(FForm).IsSysDetecting := False;
end;
//这里进来后,可以正常的 showmessage//窗体中的定义如下:
const   WM_ShowSysDetect              = WM_USER + $040;           //显示系统配置检测结果
procedure ShowSysConf(var Msg: TMessage); message WM_ShowSysDetect;//消息处理事件
procedure TFrmSysDetect.ShowSysConf(var Msg: TMessage);
begin
  Case Msg.WParam of
    0: //DirectX版本检测
      begin
        if Msg.LParam = 1 then
          imgDXState.Picture := imgYes.Picture;
        if Msg.LParam = 0 then
          imgDXState.Picture := imgNo.Picture;
        imgDXState.Visible := True;
        GSystemParam.DirectX := Integer(True);
//        ShowMessage(IntToStr(Msg.WParam));
      end;
    1: //MediaPlayer版本检测
      begin
        if Msg.LParam = 1 then
          imgMPState.Picture := imgYes.Picture;
        if Msg.LParam = 0 then
          imgMPState.Picture := imgNo.Picture;
        imgMPState.Visible := True;
        GSystemParam.MediaPlayer := Integer(True);
//        ShowMessage(IntToStr(Msg.WParam));
      end;
    2: //声卡检测
      begin
        if Msg.LParam = 1 then
          imgAUState.Picture := imgYes.Picture;
        if Msg.LParam = 0 then
          imgAUState.Picture := imgNo.Picture;
        imgAUState.Visible := True;
        GSystemParam.SoundCard := Integer(True);
//        ShowMessage(IntToStr(Msg.WParam));
      end;
  end;
end;在窗体显示时启动线程:
TSysDetectThd.Create(Self);现在问题是,为何单步跟踪时就可以进入到消息处理事件ShowSysConf中执行,如果不单步,则只能接收到第一个消息,也就是只能showmessage(0),然后程序界面就S在那里了,请问是否消息没有同步?要如何做?感觉很奇怪,在D7下面很正常,放到2007下面就不行了

解决方案 »

  1.   

    其实没有必要把窗体加进去,通过消息完全就可以实现
    在线程单元定义和窗体单元做相同定义Const
    WM_DOSOME = WM_USER + 100;
    线程中直接只要取到窗体句柄即可。
    PostMessage(窗体句柄,WM_USER ,1,1);
    即可,在线程中不要用SHOWMESSAGE之类
      

  2.   

    上面写错,我经常这样用
    PostMessage(窗体句柄,WM_DOSOME ,1,1); 
      

  3.   

    showmessage 只是想看下它到底有没有发消息出去,Postmessage() 中的窗体句柄是创建线程时赋过去的..
    至于uses,只是将消息定义在窗体单元中,没有定义到线程单元中..
      

  4.   

    你的线程不是循环的,只会执行一次,所以只会PostMessage一次,那么也只会执行ShowSysConf一次,所以也只会showmessage(0)一次,你的程序没有问题~~!
      

  5.   

    一次就够了啊,每次创建窗体时都会创建线程,在线程里不止 PostMessage 了一次..是三次
    晕S我了,单个程序没问题,很正常的,集成到软件里面就接收不到,