在程序的一些循环中,程序自身是不会响应外界(windwos)的消息的。如果使用了Application.ProcessMessages,那么可以程序一旦响应了由其传递过来的消息,就可以马上终止循环的进行了

解决方案 »

  1.   

    防止进程阻塞,使程序能够响应消息队列中的其他事件。比如你下载一个大文件的时候,还未下载成功,那么整个程序可能阻塞在这里,而不响应其他事件。但是,如果你在下载过程中,使用了processmessages就可以在下载的同时处理其他事情,响应鼠标事件等。procedure ProcessMessages;
    DescriptionCall ProcessMessages to permit the application to process messages that are currently in the message queue. ProcessMessages cycles the Windows message loop until it is empty, and then returns control to the application.Note: Neglecting message processing affects only the application calling ProcessMessages, not other applications. In lengthy operations, calling ProcessMessages periodically allows the application to respond to paint and other messages.
    Note: ProcessMessages does not allow the application to go idle, whereas HandleMessage does.
      

  2.   

    在一个大型的循环中,操作系统只负责处理循环,没有处理其他事情。加入该句之后可以处理其他事情。例如
    for i:=1 to 10000 do
    label1.caption:=inttostr(i);
    则在程序执行过程中你看不到label在变化,只有等到执行完毕以后才有变化。如果改变一下程序
    for i:= 1 to 10000 do 
    begin
    label1.caption:=inttostr(i);
    Application.ProcessMessages;
    end;
    再看一下执行效果
      

  3.   

    我发现,循环时cpu的利用率是100%