delphi10.4.2安卓线程的实例代码
时间: 2023-10-10 12:15:48 浏览: 104
下面是一个简单的 Delphi 10.4.2 安卓线程实例代码:
```delphi
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls;
type
TMyThread = class(TThread)
private
FLabel: TLabel;
FValue: Integer;
public
constructor Create(ALabel: TLabel; AValue: Integer);
procedure Execute; override;
property Value: Integer read FValue;
end;
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
FThread: TMyThread;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
constructor TMyThread.Create(ALabel: TLabel; AValue: Integer);
begin
inherited Create(True);
FLabel := ALabel;
FValue := AValue;
end;
procedure TMyThread.Execute;
var
I: Integer;
begin
for I := 1 to FValue do
begin
Sleep(1000); // 模拟一些耗时的操作
Synchronize(
procedure
begin
FLabel.Text := Format('已完成 %d 秒', [I]);
end
);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
FThread := TMyThread.Create(Label1, 10);
FThread.Start;
end;
end.
```
这段代码中,我们定义了一个 `TMyThread` 类,它继承自 `TThread`。在 `TMyThread` 类中,我们定义了一个 `Value` 属性,用于存储要执行的操作次数。在 `Execute` 方法中,我们使用 `for` 循环模拟一些耗时的操作,并通过 `Synchronize` 方法更新 UI 界面。在主界面中,当用户点击 `Button1` 按钮时,我们创建了一个 `TMyThread` 对象,并调用 `Start` 方法启动线程。
阅读全文
相关推荐













