TerminateThread杀不了线程怎么回事

TerminateThread杀不了线程怎么回事

这个我试过,跟中止进程类似,用API: 1.取线程中止码:GetExitCodeThread((void *)thread->Handle,&ExitCode); 2.强行中止: TerminateThread((void *)thread->Handle,ExitCode);如此而已 这是一个死循环线程:__fastcall MyThread::MyThread(bool CreateSuspended) : TThread(CreateSuspended){}//---------------------------------------------------------------------------extern int js;void __fastcall MyThread::Execute(){ //---- Place thread code here ---- while (true) js++; // 死循环,不停地计数}======================================================//下面是主程序:#include #pragma hdrstop #include "Unit1.h"#include "Unit2.h"//---------------------------------------------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"TForm1 *Form1;int js=0; // 全局变量 jsMyThread *thread=NULL;//---------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){}//---------------------------------------------------------------------------void __fastcall TForm1::Timer1Timer(TObject *Sender){ Edit1->Text=js; // 时钟不断显示js值}//---------------------------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender) // 运行线程{ if (thread==NULL) { thread= new MyThread(true); thread->Priority =tpHigher; thread->Resume(); // 线程运行 }}//---------------------------------------------------------------------------void __fastcall TForm1::Button2Click(TObject *Sender) // 中止线程{ if (thread!=NULL) { unsigned long ExitCode; if (!GetExitCodeThread((void *)thread->Handle,&ExitCode)) ShowMessage("得不到退出码,中止失败!"); else { if (TerminateThread((void *)thread->Handle,ExitCode)) { delete thread; thread=NULL; } else ShowMessage("中止失败!"); } }} 中止线程后,Edit1的值就不再变化