delphi 重复执行 协程;doroutine – repeat

代码示例

重复执行的协程是【智能】,是上一次任务【完成后】,间隔 x 毫秒执行下一次任务;

unit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, doroutine, System.Skia,
  Vcl.Skia;

type
  TFormMain = class(TForm)
    Memo1: TMemo;
    button1: TButton;
    SkLabel1: TSkLabel;
    procedure button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure helloWorld(task: TTask);
  end;

var
  FormMain: TFormMain;

implementation

{$R *.dfm}

procedure TFormMain.FormCreate(Sender: TObject);
begin
  ReportMemoryLeaksOnShutdown := True;
end;

procedure TFormMain.helloWorld(task: TTask);
begin
  Memo1.Lines.Add(Format('hello world,线程ID: %s', [TThread.Current.ThreadID.ToString]));
end;

procedure helloWorld2(task: TTask);
begin
  OutputDebugString(PChar(Format('hello world,线程ID: %s', [TThread.Current.ThreadID.ToString])));
end;

procedure TFormMain.button1Click(Sender: TObject);
begin
  Memo1.Lines.Add(Format('当前主线程的ID:%s', [TThread.Current.ThreadID.ToString]));
  task.ui(helloWorld).interval(1000).start;
  task.bg(helloWorld2).interval(1000).start;
end;

end.

效果图

来源链接:https://www.cnblogs.com/feng88/p/18643885

请登录后发表评论

    没有回复内容