W
windthunder
Unregistered / Unconfirmed
GUEST, unregistred user!
现有两个线程,在线程2结束时,中断线程1,但以下代码运行完后,总显示线程以未中断
procedure TcreatuserIC.Button1Click(Sender: TObject);
//创建线程1
{因为FreeOnTerminate为true,执行完Execute方法后线程将释放}
begin
if (IC1 = nil )or (IC1active = false) then
//确认线程1没有执行
begin
IC1 := TIC.CreateIt(Gauge1);
//参数为进度条
IC1active := true;
end
else
showmessage('thread1 still executing');
end;
procedure TcreatuserIC.Button3Click(Sender: TObject);
//创建线程2
begin
if (IC2 = nil )or (IC2active = false) then
//确认线程2没有执行
begin
IC2 := TuserIC.CreateIt(userchildwin.Createuser);
//参数为发卡数据
IC2active := true;
end
else
showmessage('thread2 still executing');
end;
procedure TcreatuserIC.Button2Click(Sender: TObject);
//终止线程2
begin
if (IC1 <> nil )or (IC1active = true) then
//检查线程1有没有执行
begin
IC1.Terminate
end
else
showmessage('thread not executing');
end;
procedure TcreatuserIC.Button4Click(Sender: TObject);
//终止线程2
begin
if (IC2 <> nil )or (IC2active = true) then
//检查线程2有没有执行
begin
IC2.Terminate;
end
else
showmessage('thread not executing');
end;
procedure TcreatuserIC.Threaddone(var amessage: tmessage);
//跟踪线程是否执行完毕
begin
if (IC1 <> nil )and (IC1.ThreadID = cardinal(amessage.WParam )) then
//这段我看不懂!!!!!
begin
IC1active := false;
end;
if (IC2 <> nil )and (IC2.ThreadID = cardinal(amessage.WParam )) then
begin
IC2active := false;
end;
end;
// 打开线程
procedure TcreatuserIC.FormActivate(Sender: TObject);
begin
Button1Click(Sender);
// 线程1
Button3Click(Sender);
// 线程2
end;
// 点击按扭关闭窗体
procedure TcreatuserIC.Button5Click(Sender: TObject);
begin
self.close;
end;
// 窗体关闭时关闭线程
procedure TcreatuserIC.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Button2Click(Sender);
// 终止线程1
Button4Click(Sender);
// 终止线程2
end;
/////////////////以下是建立线程的库文件
............
constructor 线程1.createIt(progbar : TGauge);
begin
inherited create(true);
// 创建一个线程,但不立即执行
freeonterminate := true;
// 线程终止时释放线程对象
PB := Progbar;
// 引用进程条
priority := tpnormal;
synchronize(InitprogressBar);
// 调用InitProgressbar 方法
suspended := false;
// 唤醒线程
end;
destructor 线程1.Destroy ;
begin
postmessage(creatuserIC.handle,wm_Threaddonemsg,self.ThreadID ,0);
//当一个线程执行完毕,邮寄消息给主窗体
inherited destroy;
end;
procedure 线程1.Execute;
begin
......
end;
constructor 线程2.createIt(progbar : TGauge);
begin
inherited create(true);
// 创建一个线程,但不立即执行
freeonterminate := true;
// 线程终止时释放线程对象
priority := tpnormal;
.........
suspended := false;
// 唤醒线程
end;
destructor 线程2.Destroy ;
begin
postmessage(creatuserIC.handle,wm_Threaddonemsg,self.ThreadID ,0);
//当一个线程执行完毕,邮寄消息给主窗体
inherited destroy;
上一个窗体.Button2.Click;
上一个窗体.button5.click;
end;
procedure 线程2.Execute;
begin
......
end;
procedure TcreatuserIC.Button1Click(Sender: TObject);
//创建线程1
{因为FreeOnTerminate为true,执行完Execute方法后线程将释放}
begin
if (IC1 = nil )or (IC1active = false) then
//确认线程1没有执行
begin
IC1 := TIC.CreateIt(Gauge1);
//参数为进度条
IC1active := true;
end
else
showmessage('thread1 still executing');
end;
procedure TcreatuserIC.Button3Click(Sender: TObject);
//创建线程2
begin
if (IC2 = nil )or (IC2active = false) then
//确认线程2没有执行
begin
IC2 := TuserIC.CreateIt(userchildwin.Createuser);
//参数为发卡数据
IC2active := true;
end
else
showmessage('thread2 still executing');
end;
procedure TcreatuserIC.Button2Click(Sender: TObject);
//终止线程2
begin
if (IC1 <> nil )or (IC1active = true) then
//检查线程1有没有执行
begin
IC1.Terminate
end
else
showmessage('thread not executing');
end;
procedure TcreatuserIC.Button4Click(Sender: TObject);
//终止线程2
begin
if (IC2 <> nil )or (IC2active = true) then
//检查线程2有没有执行
begin
IC2.Terminate;
end
else
showmessage('thread not executing');
end;
procedure TcreatuserIC.Threaddone(var amessage: tmessage);
//跟踪线程是否执行完毕
begin
if (IC1 <> nil )and (IC1.ThreadID = cardinal(amessage.WParam )) then
//这段我看不懂!!!!!
begin
IC1active := false;
end;
if (IC2 <> nil )and (IC2.ThreadID = cardinal(amessage.WParam )) then
begin
IC2active := false;
end;
end;
// 打开线程
procedure TcreatuserIC.FormActivate(Sender: TObject);
begin
Button1Click(Sender);
// 线程1
Button3Click(Sender);
// 线程2
end;
// 点击按扭关闭窗体
procedure TcreatuserIC.Button5Click(Sender: TObject);
begin
self.close;
end;
// 窗体关闭时关闭线程
procedure TcreatuserIC.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Button2Click(Sender);
// 终止线程1
Button4Click(Sender);
// 终止线程2
end;
/////////////////以下是建立线程的库文件
............
constructor 线程1.createIt(progbar : TGauge);
begin
inherited create(true);
// 创建一个线程,但不立即执行
freeonterminate := true;
// 线程终止时释放线程对象
PB := Progbar;
// 引用进程条
priority := tpnormal;
synchronize(InitprogressBar);
// 调用InitProgressbar 方法
suspended := false;
// 唤醒线程
end;
destructor 线程1.Destroy ;
begin
postmessage(creatuserIC.handle,wm_Threaddonemsg,self.ThreadID ,0);
//当一个线程执行完毕,邮寄消息给主窗体
inherited destroy;
end;
procedure 线程1.Execute;
begin
......
end;
constructor 线程2.createIt(progbar : TGauge);
begin
inherited create(true);
// 创建一个线程,但不立即执行
freeonterminate := true;
// 线程终止时释放线程对象
priority := tpnormal;
.........
suspended := false;
// 唤醒线程
end;
destructor 线程2.Destroy ;
begin
postmessage(creatuserIC.handle,wm_Threaddonemsg,self.ThreadID ,0);
//当一个线程执行完毕,邮寄消息给主窗体
inherited destroy;
上一个窗体.Button2.Click;
上一个窗体.button5.click;
end;
procedure 线程2.Execute;
begin
......
end;