gpt4 book ai didi

delphi - FMX Delphi 10.2 在执行任务时显示表单或请等待面板

转载 作者:行者123 更新时间:2023-12-03 15:49:29 25 4
gpt4 key购买 nike

我有几个关于单击按钮的任务。

例如。

  1. 显示表单或请等待面板....
  2. 从数据库加载数据(持续时间 5-10 秒)
  3. 清除所有 TEdit 字段
  4. 隐藏表单或请等待面板....
  5. ShowMessage('已完成')

是否可以在单击按钮后显示请等待面板或表单,并在全部完成后隐藏该面板。

如何同步执行任务一项一项。

或任何其他简单的解决方案。

最佳答案

这是一个创建“占位符”的简单示例,如下所示:

enter image description here

矩形具有黑色背景,并包含与中心对齐的布局;在里面你可以找到一个标签(与 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,这样您就不会立即看到该矩形。请注意,我在弧形组件中创建了一个动画,以便您可以看到它旋转:

enter image description here

通过这种方式,您可以模仿加载旋转器。然后我在表单的 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 事件处理程序中!

关于delphi - FMX Delphi 10.2 在执行任务时显示表单或请等待面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52681088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com