要求一等待窗口,能够在一比较耗时代码执行2秒后再显示,如果比较耗时的过程在2秒内已经执行完毕则不再显示等待窗口.如果比较耗时的过程在2秒后还没有执行完则显示等待窗口.
主程序代码
procedure TForm1.Button1Click(Sender: TObject);
var
  i:Integer;
begin
  ShowWaitForm('程序正在执行,请稍候.....');
  for i:=0 to 10000 do
    Memo1.Lines.Add(IntToStr(i));
  HideWaitForm;
end;ShowWaitForm的代码
unit FormWait;interfaceuses
  Windows, Classes, Controls, Forms,
   ExtCtrls, StdCtrls, ComCtrls;type
  TFrmWait = class(TForm)
    Animate1: TAnimate;
    Label1: TLabel;
    Timer1: TTimer;
    Bevel1: TBevel;
    procedure Timer1Timer(Sender: TObject);
  private
    function GetMsg: string;
    procedure SetMsg(const Value: string);
    { Private declarations }
  public
    property Msg: string read GetMsg write SetMsg;
    { Public declarations }
  end;  TWaitForm = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
  public
    AForm: TFrmWait;
  end;
procedure ShowWaitForm(AText:String);
procedure HideWaitForm;
var
  FrmWait: TFrmWait;implementation{$R *.dfm}
procedure SHWaitForm(AText:String='');
begin
  // 执行等待窗口的文字
  if not assigned(FrmWait) then
    FrmWait := TFrmWait.Create(Application);
  FrmWait.Msg := AText;
end;procedure ShowWaitForm(AText:String);
begin
  SHWaitForm(AText);
end;procedure HideWaitForm;
begin
  SHWaitForm('');
end;
{ TWaitForm }procedure TWaitForm.Execute;
begin
  { Place thread code here }
end;function TFrmWait.GetMsg: string;
begin
  Result := Label1.Caption;
end;procedure TFrmWait.SetMsg(const Value: string);
begin
  Label1.Caption := Value;  if Value = '' then
  begin
    Animate1.Active := False;
    Timer1.Enabled := False;
    Hide;
  end
  else
  begin
    if not Self.Showing then
      show;
    if not Animate1.Active then
      Animate1.Active := True;
 //   Timer1.Enabled := True;
    Self.Update;
  end;end;procedure TFrmWait.Timer1Timer(Sender: TObject);
begin
  if self.Showing then
  begin
//      Application.ProcessMessages;
    RedrawWindow(self.Handle, nil, 0, RDW_FRAME +
      RDW_INVALIDATE +
      RDW_ALLCHILDREN + RDW_NOINTERNALPAINT);
  end;
end;
应该怎样修改ShowWaitForm才能实现: 能够在一比较耗时代码执行2秒后再显示,如果比较耗时的过程在2秒内已经执行完毕则不再显示等待窗口.如果比较耗时的过程在2秒后还没有执行完则显示等待窗口.

解决方案 »

  1.   

    在显示窗体上放一TIMER设置时间为两秒,事件中写CLOSE就行了.
      

  2.   

    用TIMER控制渐显,网上Demo应该很多,
      

  3.   

    用Timer事件不行.
    在主程序中调用不显示待等窗口,主程序中的任务运行完后才显示等待窗口
      

  4.   

    将那个较耗时的操作写成线程,然后主程序中的代码顺序调整为:begin
      ......
      定时器.Enabled := True;
      with 线程类.Create(True, .....) do
      begin
        OnTerminate := MyThreadOnTerminate;
        FreeOnTerminate := True;
        Resume;
      end;
      ......
    end;procedure MyThreadOnTerminate(Sender: TObject);
    begin
      定时器.Enabled := False;
    end;
      

  5.   

    To:jadeluo(秀峰) 
      试过了,用定时器的方法不管用
      

  6.   

    你自己试一下吧, 当创建的线程等待时间超过2500ms时, 将显示出'请耐心等待'的提示:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        Timer1: TTimer;
        Memo1: TMemo;
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
      end;  TTestThread = class(TThread)
      private
        FDelay : Integer;
        FMemo  : TMemo;
        FTimer : TTimer;
        FStatus: String;
        procedure RefreshStatus();
      protected
        procedure Execute; override;
      public
        constructor Create(Suspended: Boolean;  iDelay: Integer;  AMemo: TMemo;  ATimer: TTimer);
      end;
    var
      Form1: TForm1;implementation{$R *.dfm}constructor TTestThread.Create(Suspended: Boolean;  iDelay: Integer;  AMemo: TMemo;  ATimer: TTimer);
    begin
      inherited Create(Suspended);
      FDelay := iDelay;
      FMemo := AMemo;
      FTimer := ATimer;
    end;procedure TTestThread.RefreshStatus();
    begin
      FMemo.Lines.Add(FStatus);
    end;procedure TTestThread.Execute;
    var
      iPosition : Integer;
    begin
      FStatus := Format('创建一个耗时%dms的线程', [FDelay]);
      Synchronize(RefreshStatus);
      FTimer.Enabled := True;
      Sleep(FDelay);
      FStatus := Format('耗时%dms的线程执行结束', [FDelay]);
      Synchronize(RefreshStatus);
      FTimer.Enabled := False;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Timer1.Enabled := False;
      Timer1.Interval := 2500;
    end;procedure TForm1.Timer1Timer(Sender: TObject);
    begin
      ShowMessage ('请耐心等待');
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      with TTestThread.Create(True, (Random(4)+ 1) * 1000, Memo1, Timer1) do
      begin
        FreeOnTerminate := True;
        Resume;
      end;
    end;end.