天天看點

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

一點點筆記,以便以後翻閱。

繼續閱讀