天天看点

Delphi 线程的开始、暂停、继续、停止

RAD Studio 10.2.3 测试√

本文代码仓库地址: gitee码云CSDN笔记仓库地址

新建VCL窗体应用程序

控件:

Button

*6、

Memo

Delphi 线程的开始、暂停、继续、停止

窗体代码

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  work: Twork;

procedure Demo_xy01;
var
  num: Integer;
begin
  for num := 1 to 10 do
  begin
    Form1.Memo1.Lines.Add(num.ToString + ':procedure Demo_xy01');
    // 下面个两种都可以达到延时的效果
    Sleep(500);
    // 线程休眠
    TThread.Sleep(500);
  end;
  Form1.Memo1.Lines.Add('procedure Demo_xy01:我已经执行完毕了哦');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Add('匿名线程开始');
  // 匿名线程01
  TThread.CreateAnonymousThread(Demo_xy01).Start;
  // 当上面线程开始执行后,主线程会继续向下执行
  Memo1.Lines.Add('我和上面的进程同时进行的哦!!!');
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  // 匿名线程02
  TThread.CreateAnonymousThread(
    // 匿名函数
    procedure
    var
      num: Integer;
    begin
      Memo1.Lines.Add('匿名线程开始');
      for num := 1 to 10 do
      begin
        Form1.Memo1.Lines.Add(num.ToString + ':Button3Click');
        Sleep(1000);
      end;
      Form1.Memo1.Lines.Add('Button3Click:我已经执行完毕了哦');
    end).Start;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  try
    // 线程开始
    work.Start;
    Memo1.Lines.Add('线程开始:Button4Click');
  except
    on E: Exception do
      ShowMessage('线程启动失败!');
  end;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  try
    // 暂停线程
    work.Suspended := True;
    Memo1.Lines.Add('线程暂停');
  except
    on E: Exception do
      Memo1.Lines.Add('线程暂停失败');
  end;

end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  // 继续线程
  work.Suspended := False;
  Memo1.Lines.Add('线程继续');
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
  try
    // 线程停止
    // 2021年4月16日11:14:35[Update]更改
    work.Terminate;  // 添加这句才是线程终止,下面的是线程终止的时候释放
    work.FreeOnTerminate := True;
    Memo1.Lines.Add('线程停止');
  except
    on E: Exception do
      Memo1.Lines.Add('线程停止失败!');
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // True:表示线程创建完成对象后【先挂起】
  // False:表示线程创建完成对象后【立即执行】
  work := Twork.Create(True);
  Memo1.Lines.Add('线程创建成功并挂起!')
end;

end.
           

新建多线程对象

【点击项目】–【新建】–【其他】–【多线程对象】–【确定】
Delphi 线程的开始、暂停、继续、停止

uThreads 单元文件代码

unit uThreads;

interface

uses
  System.SysUtils,
  System.Classes;

type
  Twork = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
  public
    procedure P_xy01;
  end;

implementation

uses Unit1;

{
  Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,

  Synchronize(UpdateCaption);

  and UpdateCaption could look like,

  procedure Twork.UpdateCaption;
  begin
  Form1.Caption := 'Updated in a thread';
  end;

  or

  Synchronize(
  procedure
  begin
  Form1.Caption := 'Updated in thread via an anonymous method'
  end
  )
  );

  where an anonymous method is passed.

  Similarly, the developer can call the Queue method with similar parameters as
  above, instead passing another TThread class as the first parameter, putting
  the calling thread in a queue with the other thread.

}

{ Twork }

procedure Twork.Execute;
begin
  { Place thread code here }
  // 在这里放置线程代码
  P_xy01();
end;

procedure Twork.P_xy01;
var
  num: Integer;
begin
  Form1.Memo1.Lines.Add('我的线程编号:' + Self.ThreadID.ToString);
  for num := 1 to 100 do
  begin
    if FreeOnTerminate then
    begin
      Exit;
    end;
    Form1.Memo1.Lines.Add(num.ToString + ':procedure Twork.P_xy01');
    TThread.Sleep(500);
    // 2021年4月16日11:16:53[Update]可以在这里添加判断线程是否终止,终止了就跳出循环
    if Terminated then
      Break;
  end;
  Form1.Memo1.Lines.Add('procedure Twork.P_xy01:我已经执行完毕了哦');
end;

end.
           
线程池类:TThreadPool

一点点笔记,以便以后翻阅。

继续阅读