您现在的位置:首页 >> 基础算法 >> window基础 >> 内容

Delphi中使用Application.ProcessMessagess是否要慎重考虑

时间:2011/9/3 15:29:42 点击:

  核心提示:600) this.width = 600;'>{in MyForm:}WorkLevel : integer;{OnCreate:}WorkLevel := 0;procedure TForm1.W...

600) this.width = 600;">

{in MyForm:}
  WorkLevel : integer;
{OnCreate:}
  WorkLevel := 0;

procedure TForm1.WorkBtnClick(Sender: TObject) ;
var
  cycle : integer;
begin
  inc(WorkLevel) ;
  for cycle := 1 to 5 do
  begin
    Memo1.Lines.Add('- Work ' + IntToStr(WorkLevel) + ', Cycle ' + IntToStr(cycle)) ;
    Application.ProcessMessages;
    sleep(1000) ; // or some other work
  end;
  Memo1.Lines.Add('Work ' + IntToStr(WorkLevel) + ' ended.') ;
  dec(WorkLevel) ;
end;

连续2次点击按钮,上面代码当去掉Application.ProcessMessages的时候输出结果完全不同

下面的范例代码同理:

procedure OnClickFileWrite() ;
var myfile := TFileStream;
begin
  myfile := TFileStream.create('myOutput.txt') ;
  try
    while BytesReady > 0 do
    begin
      myfile.Write(DataBlock) ;
      dec(BytesReady,sizeof(DataBlock)) ;
      DataBlock[2] := #13; {test line 1}
      Application.ProcessMessages;
      DataBlock[2] := #13; {test line 2}
    end;
  finally
    myfile.free;
  end;
end;

作者:站长 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号