码迷,mamicode.com
首页 > 编程语言 > 详细

终止TTask.Run启动的线程

时间:2019-02-13 00:41:30      阅读:513      评论:0      收藏:0      [点我收藏+]

标签:procedure   一个   cti   .class   create   color   creat   bool   dia   

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启动的线程

标签:procedure   一个   cti   .class   create   color   creat   bool   dia   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!