Y
yitang
Unregistered / Unconfirmed
GUEST, unregistred user!
写了个简单的线程作测试,但碰到了一些不解的问题
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,bytearray, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
Function getthreadcount():word;
private
{ Private declarations }
procedure threaddestroy(Sender:Tobject);
public
{ Public declarations }
end;
var
Form1: TForm1;
ThreadCount:word;
implementation
uses unit2;
{$R *.dfm}
procedure Tform1.threaddestroy(Sender:Tobject);
begin
inc(Threadcount,-1);
//showmessage(inttostr(ThreadCount));这里的ThreadCount值的确会递减
end;
function TForm1.getthreadcount():word;
begin
result:=threadcount;
end ;
procedure TForm1.Button1Click(Sender: TObject);
var
n:word;
begin
ThreadCount:=5;//这里简化了,只是想说明最终会创建5个线程
for n:=1 to 5do
begin
with mythread.createdo
//线程已经inherited create(false),FreeonTerminate:=true
onterminate:=threaddestroy;
end;
sleep(10000);//确保所有线程都有足够时间完成! 线程定义很简单!
showmessage(inttostr(getthreadcount));//问题:这里显示的是5
showmessage(inttostr(getthreadcount));// 这里显示的是0;
end;
end.
大家帮看一下,我想了一个下午也没有找出原因!谢谢!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,bytearray, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
Function getthreadcount():word;
private
{ Private declarations }
procedure threaddestroy(Sender:Tobject);
public
{ Public declarations }
end;
var
Form1: TForm1;
ThreadCount:word;
implementation
uses unit2;
{$R *.dfm}
procedure Tform1.threaddestroy(Sender:Tobject);
begin
inc(Threadcount,-1);
//showmessage(inttostr(ThreadCount));这里的ThreadCount值的确会递减
end;
function TForm1.getthreadcount():word;
begin
result:=threadcount;
end ;
procedure TForm1.Button1Click(Sender: TObject);
var
n:word;
begin
ThreadCount:=5;//这里简化了,只是想说明最终会创建5个线程
for n:=1 to 5do
begin
with mythread.createdo
//线程已经inherited create(false),FreeonTerminate:=true
onterminate:=threaddestroy;
end;
sleep(10000);//确保所有线程都有足够时间完成! 线程定义很简单!
showmessage(inttostr(getthreadcount));//问题:这里显示的是5
showmessage(inttostr(getthreadcount));// 这里显示的是0;
end;
end.
大家帮看一下,我想了一个下午也没有找出原因!谢谢!