关于Delphi中的application.ProcessMessages问题(处理事件)

关于Delphi中的application.ProcessMessages问题(处理事件)
━━━━━━━━━━━━━━━━━━━━━━━━━━

很多资料中提到,Delphi中的application.ProcessMessages相当于VB中的Doevents函数(转让进程控制),可是,在实际应用中,却不是这么回事。例:  
(VB   Code)  
Private   Sub   Command1_Click()    
        Dim   i   As   Integer    
        Dim   j   As   Integer    
        Text1.Text   =   0    
        For   i   =   1   To   10000    
                For   j   =   1   To   10000    
                        DoEvents    
                        Text1.Text   =   Val(Text1.Text)   +   1    
                Next    
          Next  
End   Sub    
   
Private   Sub   Command2_Click()    
          MsgBox   1    
          Unload   Me  
End   Sub  
=================================================================  
(Delphi   Code)  
procedure   TForm1.Button1Click(Sender:   TObject);  
var   i,j:integer;  
begin    
          edit2.Text:='0';    
          for   i:=1   to   10000   do    
                  Begin    
                          for   j:=1   to   10000   do    
                                Begin    
                                        application.ProcessMessages;          
                                        edit1.Text:=inttostr(strtoint(edit2.text)+   1);    
                                end;    
                  end;  
end;  
   
procedure   TForm1.Button2Click(Sender:   TObject);  
begin    
        showmessage('1');    
        Close;  
end;  
=================================  
当点击VB中的Command2按钮时,马上弹出消息框并关掉应用程序(或点窗体关闭键时会即时关掉程序),可是,点Delphi中的Button2Click时,虽然也弹出消息框,可是应用程序并不关闭,仍在计算,即使点窗体关闭键也要计算完后才关掉。在一些情况下,可能要用到大循环,因此,要给用户提供随时中止程序的功能,那么,在Delphi中,如何才能实现?
━━━━━━━━━━━━━━━━━━━━━━━━━━
procedure   TForm1.Button2Click(Sender:   TObject);  
begin    
        showmessage('1');    
        Application.Terminate;     //改为这个。  
end;
━━━━━━━━━━━━━━━━━━━━━━━━━━
procedure   TForm1.Button1Click(Sender:   TObject);  
var   i,j:integer;  
begin    
          edit2.Text:='0';    
          for   i:=1   to   10000   do    
                  Begin    
                          for   j:=1   to   10000   do    
                                Begin    
                                        application.ProcessMessages;          
                                        if   Application.Terminated   then   Exit;  
                                        edit1.Text:=inttostr(strtoint(edit2.text)+   1);    
                                end;    
                  end;  
end;  
   
procedure   TForm1.Button2Click(Sender:   TObject);  
begin    
        showmessage('1');    
        Application.Terminate;  
end;  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值