求DELPHI多线程例程
最近在学DELPHI,程序没有多线程很麻烦,自己找资料看半天也没看懂,希望高手写个例程给我看看。最好是用一个线程累加数并显示在界面上,按停止按钮就可以结束线程。谢谢,谢谢...
最近在学DELPHI,程序没有多线程很麻烦,自己找资料看半天也没看懂,希望高手写个例程给我看看。最好是用一个线程累加数并显示在界面上,按停止按钮就可以结束线程。谢谢,谢谢!!
展开
1个回答
展开全部
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hloopHandle:Thandle ; //线程 句柄
dloopThreadID:DWORD ; //线程 id
function doloop(P:pointer):Longint;stdcall;
implementation
{$R *.dfm}
//启动线程处理
procedure TForm1.Button1Click(Sender: TObject);
begin
hloopHandle := CreateThread(nil,0,@doloop,nil,0,dloopThreadID);
if hloopHandle=0 then
begin
messagebox(Handle,'Didn’t Create a Thread',nil,MB_OK);
abort;
end;
end;
//终止线程
procedure TForm1.Button2Click(Sender: TObject);
begin
if hloopHandle<>0 then
TerminateThread(hloopHandle,dloopThreadID);
end;
//线程处理函数
function doloop(P:pointer):Longint;stdcall;
var
i:integer;
begin
for i:=0 to 10000 do
begin
form1.Edit1.Text:=inttostr(i);
end;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hloopHandle:Thandle ; //线程 句柄
dloopThreadID:DWORD ; //线程 id
function doloop(P:pointer):Longint;stdcall;
implementation
{$R *.dfm}
//启动线程处理
procedure TForm1.Button1Click(Sender: TObject);
begin
hloopHandle := CreateThread(nil,0,@doloop,nil,0,dloopThreadID);
if hloopHandle=0 then
begin
messagebox(Handle,'Didn’t Create a Thread',nil,MB_OK);
abort;
end;
end;
//终止线程
procedure TForm1.Button2Click(Sender: TObject);
begin
if hloopHandle<>0 then
TerminateThread(hloopHandle,dloopThreadID);
end;
//线程处理函数
function doloop(P:pointer):Longint;stdcall;
var
i:integer;
begin
for i:=0 to 10000 do
begin
form1.Edit1.Text:=inttostr(i);
end;
end;
end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询