FMX Delphi 10.2任务执行时显示窗体或请等待面板

kognpnkq  于 2023-04-05  发布在  其他
关注(0)|答案(1)|浏览(181)

我有几个关于按钮点击的任务。
例如
1.显示表单或请等待面板....
1.从数据库加载数据(持续时间5-10秒)
1.清除所有TEdit字段
1.隐藏表单或请等待面板....

  1. ShowMessage('completed ')
    是否有可能在点击按钮后显示请等待面板或表格,并在所有完成后隐藏该面板。
    如何同步逐个执行任务。
    或者其他简单的解决方案。
ndasle7k

ndasle7k1#

这是一个简单的例子,它创建了一个“占位符”,看起来像这样:

该矩形具有黑色背景并包含与Center对齐的布局;在里面你可以找到一个标签(与Top对齐)和一个弧(与Client对齐)。代码如下:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 418
  ClientWidth = 490
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  OnCreate = FormCreate
  DesignerMasterStyle = 0
  object Rectangle1: TRectangle
    Align = Client
    Fill.Color = xFF222222
    Size.Width = 490.000000000000000000
    Size.Height = 418.000000000000000000
    Size.PlatformDefault = False
    Visible = False
    object Layout1: TLayout
      Align = Center
      Size.Width = 170.000000000000000000
      Size.Height = 102.000000000000000000
      Size.PlatformDefault = False
      TabOrder = 0
      object Label1: TLabel
        Align = Top
        StyledSettings = [Family, Size, Style]
        Size.Width = 170.000000000000000000
        Size.Height = 41.000000000000000000
        Size.PlatformDefault = False
        TextSettings.FontColor = claWhite
        TextSettings.HorzAlign = Center
        Text = 'Please wait'
        TabOrder = 0
      end
      object Arc1: TArc
        Align = Center
        Size.Width = 50.000000000000000000
        Size.Height = 50.000000000000000000
        Size.PlatformDefault = False
        Stroke.Color = claCoral
        EndAngle = -90.000000000000000000
        object FloatAnimation1: TFloatAnimation
          Enabled = True
          Duration = 1.000000000000000000
          Loop = True
          PropertyName = 'RotationAngle'
          StartValue = 0.000000000000000000
          StopValue = 360.000000000000000000
        end
      end
    end
  end
end

矩形的Visible属性被设置为False,这样你就不会立即看到矩形。请注意,我在arc组件中创建了一个动画,这样你就可以看到它在旋转:

这样你就可以模拟一个加载微调器。然后我在表单的OnCreate事件中添加了这段代码,作为你如何做到这一点的例子。

procedure TForm1.FormCreate(Sender: TObject);
begin
  TTask.Run(procedure
            begin
              TThread.Synchronize(nil, procedure
                                 begin
                                   Rectangle1.Visible := true;
                                   //Rectangle1.BringToFront;
                                   // ^ call the above if needed, just to be sure
                                   // that you'll always see the rectangle on screen 
                                 end);

              Sleep(4000);

              TThread.Synchronize(nil, procedure
                                 begin
                                   Rectangle1.Visible := false;
                                   ShowMessage('Finish!');
                                 end);
            end);
end;

Sleep(4000)模拟了一个长任务,这段代码应该替换为你的任务。实际上我会这样做:

procedure TForm1.FormCreate(Sender: TObject);
begin
  TTask.Run(procedure
            var
              arr: array [0..1] of ITask;
            begin
              TThread.Synchronize(nil, procedure
                                 begin
                                   Rectangle1.Visible := true;
                                   Rectangle1.BringToFront;
                                 end);

              arr[0] := TTask.Run(procedure
                                  begin
                                    //load data from the database
                                  end);

              arr[1] := TTask.Run(procedure
                                  begin
                                    //something else
                                  end);

              //this call is blocking but you are calling this in a worker thread!
              //your UI won't freeze and at the end you'll see the message appearing
              TTask.WaitForAll(arr);
              TThread.Synchronize(nil, procedure
                                 begin
                                   Rectangle1.Visible := false;
                                   ShowMessage('Finish!');
                                 end);
            end);
end;

当然,您应该将此代码放在ButtonClick中,而不是FormCreate事件处理程序中!

相关问题