首页 > 编程语言 > 详细

终止TTask.Run启动的线程

时间:2019-02-12 23:49:57      阅读:36      评论:0      收藏:0      [点我收藏+]

标签:pan   set   type   dia   ase   ring   label   mwc   开启   

unit Unit15;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Threading,
  kbmmwprocess, Vcl.StdCtrls, kbmMWClientDataset;

type
  TForm15 = class(TForm)
    Button1: TButton;
    kbmMWClientTransactionResolver1: TkbmMWClientTransactionResolver;
    Button2: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FTaskIsRunning: boolean;
    cnt: Integer;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form15: TForm15;

implementation

{$R *.dfm}

procedure TForm15.Button1Click(Sender: TObject);
begin
  FTaskIsRunning := true;
  TTask.Run(
    procedure
    begin
      while FTaskIsRunning do
      begin
        sleep(99);
        TThread.Synchronize(nil,
          procedure
          begin
            Label1.Caption := Thread is running... + cnt.ToString;
            inc(cnt);
          end);

      end;
    end);

end;

procedure TForm15.Button2Click(Sender: TObject);
begin
  FTaskIsRunning := false;
  cnt := 0;
end;

procedure TForm15.FormCreate(Sender: TObject);
begin
  FTaskIsRunning := false;
  cnt := 0;
end;

end.

上面的代码,运行正常,按Button1开启一个线程,Button2终止线程运行。这是一个VCL项目。

 

终止TTask.Run启动的线程

标签:pan   set   type   dia   ase   ring   label   mwc   开启   

原文:https://www.cnblogs.com/kinglandsoft/p/10367549.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号