示例代码
unit main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls, doroutine;
type
TForm3 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure doCompleted(go: TGo);
begin
if TThread.Current.ThreadID = MainThreadID then
begin
ShowMessage('主线程 执行了 完成回调');
end else begin
ShowMessage('bg线程 执行了 完成回调');
end;
end;
procedure bgSmall(go: TGo);
begin
//doSomeThing http 请求了,或其他 后台业务
Sleep(10); //模拟做事
//可看下日志
OutputDebugString(PChar(Format('线程ID: %s,做完了一个!', [TThread.Current.ThreadID.ToString])));
end;
procedure bgBig(go: TGo);
begin
//此大协程开启300个bg小协程做事
for var i := 1 to 300 do
begin
go.bg(bgSmall).start;
end;
end;
procedure TForm3.Button1Click(Sender: TObject);
begin
//开启一个大协程做事,做完 主线程 执行回调
go.bg(bgBig).onCompleteUi(doCompleted).start;
//go.bg(bgBig).onCompleteBg(doCompleted).start; 后台线程执行回调
end;
end.
效果图
