网上找了一个不挂起不占cpu的延时函数,在使用的过程中,发现在循环时不能关闭窗口,
按理说加了一句: Application.ProcessMessages;不是应该可以响应其它的消息的吗?
为啥不能响应关闭窗口的消息呢?望大神帮忙解答一下,多谢啦
delphi2010函数部分:procedure TForm2.delay(msecs:integer);        //超级延时 
var
  Tick: DWord;
  Event: THandle;
begin
  Event := CreateEvent(nil, False, False, nil);
  try
    Tick := GetTickCount + DWord(msecs);
    while (msecs > 0) and (MsgWaitForMultipleObjects(1, Event, False, msecs, QS_ALLINPUT) <> WAIT_TIMEOUT) do
    begin
      Application.ProcessMessages;     //处理事件()
      msecs := Tick - GetTickcount;
    end;
  finally
    CloseHandle(Event);
  end;
end;应用部分(这里点击了按钮后,循环不执行完是不能关闭窗口的):procedure TForm2.Button3Click(Sender: TObject);
var i:Integer;
begin
i:=0;
for I := 0 to 10 do
  begin
    ShowMessage(IntToStr(i));
    Delay(2000);
  end;
end;

解决方案 »

  1.   

    不推荐你这种写法,不挂起不占CPU的延时函数,请用定时器。
      

  2.   

    sleep(1000);
      

  3.   


    sleep是会挂起程序造成卡死的大哥.
      

  4.   

     while (msecs > 0) and (MsgWaitForMultipleObjects(1, Event, False, msecs, QS_ALLINPUT) <> WAIT_TIMEOUT) do加一个全局状态字teminateit:boolean while (not teminateit) and  (msecs > 0) and (MsgWaitForMultipleObjects(1, Event, False, msecs, QS_ALLINPUT) <> WAIT_TIMEOUT) do在窗口关闭前,把:teminateit:=true;
      

  5.   


    sleep是会挂起程序造成卡死的大哥.sleep就是等待。
      

  6.   

    不要线程也可以实现 ,看下面。procedure Delay(i_Msc:integer);
    var
           Msg:TMsg;
    begin
           //启动一个定时器,间隔1000,定时器里sendMessage    WM_USER+1000
           try
           获取现在的时间GetTickCount
            repeat
                     if    GetMessage(Msg, 0, 0,0) then
                     begin
                             if Msg.Msg=WM_USER+1000 then
                             begin
                                    获取一个时间差   
                             end;
                      end  else
                       begin
                               if Msg.Msg=WM_QUIT then
                               begin
                                       ExitProcess(0);
                                       Exit;
                               end;
                       end;
            until (定时到)
            finally
                   //关闭定时器
            end;
    end;
      

  7.   

    更正下:procedure Delay(i_Msc:integer);
    var
           Msg:TMsg;
    begin
           //启动一个定时器,间隔1000,定时器里sendMessage    WM_USER+1000
           try
           获取现在的时间GetTickCount
            repeat
                     if    GetMessage(Msg, 0, 0,0) then
                     begin
                             if Msg.Msg=WM_USER+1000 then
                             begin
                                    获取一个时间差   
                             end else
                             begin
                                     TransMessage(Msg);
                                     DispatchMessage(Msg);
                             end;
                      end  else
                       begin
                               if Msg.Msg=WM_QUIT then
                               begin
                                       ExitProcess(0);
                                       Exit;
                               end;
                       end;
            until (定时到)
            finally
                   //关闭定时器
            end;
    end;