Application.ProcessMessages

本文解释了在Delphi应用程序中使用Application.ProcessMessages的作用及重要性。通过处理消息队列,该函数确保了UI更新和其他事件能够得到及时响应,避免程序在长时间运行循环时冻结。

Application.ProcessMessages的作用是让Application去处理消息队伍中的消息。
举例说明它的用处:
你有一程序,要做一循环,
for i:= 0 to query1.recordcount-1 do
begin
做某事;
endl;
运行的时候你会发现,当转到另一程序再转回来的时候,窗体变发白了,
除了那个DBGRID。这是为什么,这是因为当你转回自己的程序的时候,系统向你发出了
FOCUS消息(不好意思,具体什么消息忘了),一般情况下,程序接收到此消息的时候
会重画先前被挡住的范围(消息里面会告诉程序要被挡住了那些范围)
但你的程序由于正在循环中,来不及处理这个消息,所以除了DBGRID(因为当记录跳动的时候
DBGRID会重画自己)处别的部份都是白的,
这时候你可以改一下你的循环那部份的代码,如下:for i:= 0 to query1.recordcount-1 do
begin
做某事;
Application.ProcessMessages
end;
使程序在做完一次循环后,就去处理它收到的消息。虽然不能实时响应系统消息,至少
也不会延迟太长时间。

至于Application.OnException,它是Application事件。
当程序有异常时会触发此事件的产生。

----------------------------------------s

 

//如果有这样一个循环, 是非常可怕的; 因为它完不了, 你得等着.
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to MaxInt do
  begin
    Text := IntToStr(i);
  end;
end;




//即使这样也无济于事, 因为在循环期间你执行不了 Button2Click
var
  b: Boolean;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  b := True;
  for i := 0 to MaxInt do
  begin
    if b then Text := IntToStr(i) else Exit;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  b := False;
end;




//如果在循环期间...
for i := 0 to MaxInt do
  begin
    if b then Text := IntToStr(i) else Exit;
    {看看还有其他什么事情发生}
  end;




//Application.ProcessMessages 就是干这个的!
var
  b: Boolean;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  b := True;
  for i := 0 to MaxInt do
  begin
    if b then Text := IntToStr(i) else Exit; Application.ProcessMessages; {!}
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  b := False;
end;

function LoadDLL(Sender: TObject): Boolean; var CreateMainForm : function: TForm; stdCall; B : Boolean; begin Result := False; B := ParentIs('BDS.EXE'); SendMessage(frmsplash.Handle, Msg_Main, 204, 4); CheckParent; DM.ServerName := ''; DM.SysType := 0; uFunc.wpGrade.ParentName := 'gPallas.exe'; uFunc.wpGrade.SysName := 'gPallas'; uFunc.wpGrade.ProjectName := GetIniValue('gPallas', '项目名称', '广东省交通工程质量综合管理平台'); try Application.ProcessMessages; frmsplash.FormStyle := fsNormal; if not uFunc.wpGrade.UpgradeStartup(True) then Exit; frmsplash.FormStyle := fsStayOnTop; Application.ProcessMessages; SendMessage(frmsplash.Handle, Msg_Main, 204, 1); frmsplash.FormStyle := fsNormal; Application.ProcessMessages; if not LoadDLL_Resource then Exit; //资源文件,与其他无关 if not B then frmsplash.FormStyle := fsStayOnTop; Application.ProcessMessages; frmsplash.FormStyle := fsNormal; Application.ProcessMessages; if not LoadDLL_wpFunc then Exit; //通用函数,与其他无关 if not B then frmsplash.FormStyle := fsStayOnTop; Application.ProcessMessages; frmsplash.FormStyle := fsNormal; Application.ProcessMessages; if not LoadDLL_weepalsc(True) then Exit; //扩展函数,关联 wpFunc.DLL、UDM if not B then frmsplash.FormStyle := fsStayOnTop; Application.ProcessMessages; if lhwpBasic = 0 then Exit; @CreateMainForm := GetProcAddress(lhwpBasic, PAnsiChar('_CreateMainForm')); if @CreateMainForm = nil then Exit; if CreateMainForm = nil then Exit; except on E: Exception do begin ShowMessage(E.Message); TerminateProcess(OpenProcess(PROCESS_TERMINATE, Bool(0), GetCurrentProcessId), 0); end; end; Result := True; end;
最新发布
10-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值