delphi多线程例子 采用createThread创建多线程

2017-09-14 11:24:03来源:CSDN作者:qq_24932309人点击

分享

一、才用createThread创建多线程,需要先声明一个多线程句柄(全局的)如:hThread:THandle;

二、声明了一个多相处句柄之后,还需要先写一个函数,在创建多线程是用来掉用的函数。

三、使用createThread创建多线程,hThread:=createThread(nil, 0, @MyThreadFun1, nil, CREATE_SUSPENDED, ID);这里线程创建起是挂起状态

createThread方法参数:

function CreateThread(
lpThreadAttributes: Pointer;           {安全设置}
dwStackSize: DWORD;                    {堆栈大小}
 lpStartAddress: TFNThreadStartRoutine; {入口函数}
 lpParameter: Pointer;                  {函数参数}
 dwCreationFlags: DWORD;                {启动选项}
 var lpThreadId: DWORD                  {输出线程 ID }
): THandle; stdcall;                     {返回线程句柄}

四、使用线程时,需要将线程唤醒。不适用是需要将线程挂起。

   ResumeThread(hThread);{唤醒线程}

  SuspendThread(hThread);{挂起线程}

程序例子:

  unit Unit1;


interface


uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;


type
  TForm1 = class(TForm)
    btn1: TButton;
    btn2: TButton;
    btn3: TButton;
    tmr1: TTimer;
    edt1: TEdit;
    btn4: TButton;
    btn5: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure tmr1Timer(Sender: TObject);
    procedure btn4Click(Sender: TObject);
    procedure btn5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;


implementation


var
  hThread: THandle;
  hThread1:THandle;
  num: Integer;
  num1:Integer;
{$R *.dfm}


{线程入口函数}


function MyThreadFun(p: Pointer): Integer; stdcall;
begin
   while True do       {假如线程不挂起,循环一直运行}
   begin
      num:=Random(1000);
   end;
   Result:=0;
end;


function MyThreadFun1(p: Pointer): Integer; stdcall;
begin
   while True do       {假如线程不挂起,循环一直运行}
   begin
      num1:=Random(1000);
   end;
   Result:=0;
end;


{第一个按钮,用于建立和挂起线程,线程挂起相当于是线程暂停}
procedure TForm1.btn1Click(Sender: TObject);
var
  ID:DWORD;
begin
   hThread:=CreateThread(nil, 0, @MyThreadFun, nil, CREATE_SUSPENDED, ID);
   hThread1:=CreateThread(nil, 0, @MyThreadFun1, nil, CREATE_SUSPENDED, ID);
   btn1.Enabled:=False;
end;


procedure TForm1.btn2Click(Sender: TObject);
begin
   ResumeThread(hThread);{唤醒线程}
end;


procedure TForm1.btn3Click(Sender: TObject);
begin
    SuspendThread(hThread);{挂起线程}
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
    tmr1.Interval:=100;
end;


procedure TForm1.tmr1Timer(Sender: TObject);
begin
    Text := IntToStr(num);
    edt1.Text:=IntToStr(num1);
end;


procedure TForm1.btn4Click(Sender: TObject);
begin
    ResumeThread(hThread1);{唤醒线程2}
end;


procedure TForm1.btn5Click(Sender: TObject);
begin
   SuspendThread(hThread1);{挂起线程2}
end;unit Unit1;


interface


uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;


type
  TForm1 = class(TForm)
    btn1: TButton;
    btn2: TButton;
    btn3: TButton;
    tmr1: TTimer;
    edt1: TEdit;
    btn4: TButton;
    btn5: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure tmr1Timer(Sender: TObject);
    procedure btn4Click(Sender: TObject);
    procedure btn5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;


implementation


var
  hThread: THandle;
  hThread1:THandle;
  num: Integer;
  num1:Integer;
{$R *.dfm}


{线程入口函数}


function MyThreadFun(p: Pointer): Integer; stdcall;
begin
   while True do       {假如线程不挂起,循环一直运行}
   begin
      num:=Random(1000);
   end;
   Result:=0;
end;


function MyThreadFun1(p: Pointer): Integer; stdcall;
begin
   while True do       {假如线程不挂起,循环一直运行}
   begin
      num1:=Random(1000);
   end;
   Result:=0;
end;


{第一个按钮,用于建立和挂起线程,线程挂起相当于是线程暂停}
procedure TForm1.btn1Click(Sender: TObject);
var
  ID:DWORD;
begin
   hThread:=CreateThread(nil, 0, @MyThreadFun, nil, CREATE_SUSPENDED, ID);
   hThread1:=CreateThread(nil, 0, @MyThreadFun1, nil, CREATE_SUSPENDED, ID);
   btn1.Enabled:=False;
end;


procedure TForm1.btn2Click(Sender: TObject);
begin
   ResumeThread(hThread);{唤醒线程}
end;


procedure TForm1.btn3Click(Sender: TObject);
begin
    SuspendThread(hThread);{挂起线程}
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
    tmr1.Interval:=100;
end;


procedure TForm1.tmr1Timer(Sender: TObject);
begin
    Text := IntToStr(num);
    edt1.Text:=IntToStr(num1);
end;


procedure TForm1.btn4Click(Sender: TObject);
begin
    ResumeThread(hThread1);{唤醒线程2}
end;


procedure TForm1.btn5Click(Sender: TObject);
begin
   SuspendThread(hThread1);{挂起线程2}
end;



最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台