use*_*doo 3 delphi firemonkey delphi-10.2-tokyo
单击按钮有几个任务。
例如。
是否可以单击按钮显示后,请等待面板或表单,并在完成所有操作后隐藏该面板。
如何同步一对一执行任务。
或任何其他简单的解决方案。
这是一个简单的示例,它创建一个看起来像这样的“占位符”:
矩形具有黑色背景并包含与对齐的布局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
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
该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;
Run Code Online (Sandbox Code Playgroud)
当然,您应该将此代码放置在ButtonClick中,而不是在FormCreate事件处理程序中!