terminatethreadVB/易语言中TerminateThread如何终止线程?

terminatethread  时间:2021-07-16  阅读:()

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 #include "Unit1.h" #include "Unit2.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; int js=0; // 全局变量 js MyThread *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的值就不再变化

delphi 里如何创建线程执行完线程后结束线程.

在Delphi中使用线程,当窗体关闭时,如果窗体中启用了线程,一般需要手动关闭,以释放资源。

常用来结束线程的代码为: PcmThrd.Terminate; PcmThrd.WaitFor; 即先触发Terminate方法,然后等待线程的结束。

这种方法要求线程不能使用 FreeOnTerminate := True; ,否则在WaitFor即将结束的时候会引发“无效句柄”的错误。

这种方法在窗体关闭的时候会等待一段事件(因为WaitFor)。

因此,如果不是在主窗体中结束线程时,其实我们可以不必使用WaitFor。

而是采用如下方法: 将FreeOnTerminate := True; 这样在窗体关闭的代码中直接调用 PcmThrd.Terminate; 即可。

注意: 如果设置了 PcmThrd.OnTerminate := SomeFunction; 那么在调用PcmThrd.Terminate;前尽量将PcmThrd.OnTerminate := nil,以免结束线程后SomeFunction中的变量出现空指针错误。

当然,这不是绝对的,需要根据具体程序而定 DELPHI 线程的终止和退出 1、自然退出 一个线程从execute()过程中退出,即意味着线程的终止,此时将调用windows的exitthread()函数来清除线程所占用的堆栈。

如果线程对象的 freeonterminate 属性设为true,则线程对象将自动删除,并释放线程所占用的资源。

2、terminate属性退出 利用线程对象的terminate属性,可以由进程或者由其他线程控制线程的退出。

只需要简单的调用该线程的terminate方法,并设直线程对象的terminate属性为true。

在线程中,应该不断监视terminate的值,一旦发现为true,则退出,例如在execute()过程中可以这样写: while not terminated do  begin   ....... end; 3、利用api 函数 线程退出的api 函数声明如下: function terminatethread(hthread:thandle;dwexitcode:dword); 这个函数会使代码立刻终止,不管程序中有没有   try....finally   机制,可能会导致错误,不到万不得已,最好不要使用。

4、挂起后释放 利用挂起线程的suspend方法,后面跟个free,也可以释放线程,例如:    thread1.suspend;  //挂起    thread1.free;     //释放 遇到的" Code:1400 无效窗口句柄 "的问题,关闭不了程序 最近写程序,遇到的" Code:1400 无效窗口句柄 "的问题,关闭不了程序?! 似乎是在线程里调用了主窗体的东西,使得释放的先后次序被打乱了,所以句柄有问题! 但是要找到问题的根源太麻烦了,这时有招必杀技! 大家要记好了,有类似的无法关闭程序的问题,一句搞定! ExitProcess(0); 简单的说就是终止自己的进程!虽然是暴力了一点,但是绝对有效! 但是这种方法不会触发onclose之类的事件,可以说是不触发任何事件,无痛无痒地结束了进程,干净利落,所以要记得在结束之前保存必要的数据,做必要的操作,最好是释放一下内存,在Win下结束进程是非常不干净的,会有内存残留。

获取线程状态 Function CheckThreadFreed(aThread: TThread): Byte; var i: DWord; IsQuit: Boolean; begin if Assigned(aThread) then begin IsQuit := GetExitCodeThread(aThread.Handle, i); if IsQuit then //If the function eeds, the return value is nonzero. //If the function fails, the return value is zero. begin if i = STILL_ACTIVE then //If the specified thread has not terminated, //the termination status returned is STILL_ACTIVE. Result := 1 else Result := 2; //aThread未Free,因为Tthread.Destroy中有执行语句 end else Result := 0; //可以用GetLastError取得错误代码 end else Result := 3; end; 快速关闭线程 //=========ThrdMain.pas================== Var hEventDead:Thandle; constructor ThrdMain.Create; begin hEventDead := CreateEvent(0,true,False,‘‘);//创建对象事件 inherited Create(False); end; //创建对象关闭事件 function ThrdMain.WaitEventDead: Boolean; begin //创建线程 var Thread_Main:ThrdMain ;(调用自我创建的线程对象) Thread_Main:=ThrdMain.Create ; //关闭线程 Thread_Main.WaitEventDead ; Thread_Main.WaitFor ; //=========ThrdMain.pas================== var hEventDead:Thandle; constructor ThrdMain.Create; begin hEventDead := CreateEvent(0,true,False,‘‘);//创建对象事件 inherited Create(False); end; //创建对象关闭事件 function ThrdMain.WaitEventDead: Boolean; begin Gbl_ReadSMS:=False; WaitForSingleObject(hEventDead,500);//表示在0.5秒内强制关闭 end; 多线程的检查,与关闭线程 procedure TDemoThread.Execute; begin inherited; if Assigned(FOnHintText) then FOnHintText(Self); end; procedure TForm1.ShowThreadDemo(Sender: TObject); var i: Integer; begin for i := 0 to 1000 do begin Memo1.Lines.Add(IntToStr(i)); end; end; procedure TForm1.Button2Click(Sender: TObject); var vI: DWord; IsQuit: Boolean; begin Demo := TDemoThread.Create(True); //True:创建时不启动线程 Demo.FreeOnTerminate := True; //设置程结束时自动释放 Demo.FOnHintText := ShowThreadDemo; //Demo.OnTerminate:= ShowThreadDemo; Demo.Resume; //启劫线程 end; procedure TForm1.FormDestroy(Sender: TObject); var vI: DWord; IsQuit: Boolean; begin if Demo <> nil then begin vi := CheckThreadFreed(Demo); //检查当前线程是否在执行 if (vi = 1) or (vi = 2) then TerminateThread(Demo.Handle, vi); //如果线程在执行则强行退出 Demo.Free; end; {if Demo<> nil then begin Demo.Terminate ; Demo.WaitFor ; end;} //等待线程结束,并终止它 end; procedure TForm1.Button1Click(Sender: TObject); begin Demo.Free; end; procedure TForm1.Button4Click(Sender: TObject); var vI: DWord; IsQuit: Boolean; begin //判断当前线程的状态 vI := CheckThreadFreed(Demo); ShowMessage(IntToStr(vI)); end;

Terminate是请求结束一个线程,那么如何真正结束一个线程呢?

推荐通过让线程运行结束返回的方法结束线程. 仅当你确切知道该线程的作用和该线程在结束时可能运行的代码的时候调用TerminateThread 来结束线程. 不然后果是它的初始化堆栈没有被释放, 占用的临界区没有释放,连接到该线程的 DLLs 没有知道该线程要结束, 而且如果目标线程管理着一个共享 DLL 的全局状态, 则该 DLL 的状态可能被毁坏, 影响到其他的 DLL 用户.

VB/易语言中TerminateThread如何终止线程?

VB6.0的。

代码如下。

单击窗体效果就显现。

================Option ExplicitPrivate Declare Function GetCurrentThread Lib "kernel32" () As LongPrivate Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, lpExitCode As Long) As LongPrivate Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As LongPrivate Sub Form_Click()Dim hT As OLE_HANDLE, lEC As LonghT = GetCurrentThreadGetExitCodeThread hT, lECTerminateThread hT, lECEnd Sub

[6.18]IMIDC:香港/台湾服务器月付30美元起,日本/俄罗斯服务器月付49美元起

IMIDC发布了6.18大促销活动,针对香港、台湾、日本和莫斯科独立服务器提供特别优惠价格最低月付30美元起。IMIDC名为彩虹数据(Rainbow Cloud),是一家香港本土运营商,全线产品自营,自有IP网络资源等,提供的产品包括VPS主机、独立服务器、站群独立服务器等,数据中心区域包括香港、日本、台湾、美国和南非等地机房,CN2网络直连到中国大陆。香港服务器   $39/...

tmhhost:暑假快乐,全高端线路,VPS直接8折,200G高防,美国gia日本软银韩国cn2香港cn2大带宽

tmhhost为2021年暑假开启了全场大促销,全部都是高端线路的VPS,速度快有保障。美国洛杉矶CN2 GIA+200G高防、洛杉矶三网CN2 GIA、洛杉矶CERA机房CN2 GIA,日本软银(100M带宽)、香港BGP直连200M带宽、香港三网CN2 GIA、韩国双向CN2。本次活动结束于8月31日。官方网站:https://www.tmhhost.com8折优惠码:TMH-SUMMER日本...

半月湾hmbcloud升级500Mbps带宽,原生VPS,$4.99/月

关于半月湾HMBCloud商家之前也有几篇那文章介绍过这个商家的产品,对于他们家的其他产品我都没有多加留意,而是对他们家的DC5机房很多人还是比较喜欢的,这个比我们有些比较熟悉的某商家DC6 DC9机房限时,而且半月湾HMBCloud商家是相对便宜的。关于半月湾DC5机房的方案选择和介绍:1、半月湾三网洛杉矶DC5 CN2 GIA同款DC6 DC9 1G内存 1TB流量 月$4.992、亲测选择半...

terminatethread为你推荐
mdmMDM产品是如何获取管理终端的权限的?一物一码一码归一码的上句是什么?onboardon board是什么意思?weakhashmapJava///map的父类是?rdl电脑主机上的dvd+rdl是什么意思mindmanager破解版求mindmanager 2019 的注册机deviceidAndroid里DeviceId和AndroidId都是什么意思?ruby语言Ruby语言输入方法法faq是什么意思fans是什么意思?数据分析报告范文数据分析报告怎么写
cc域名 cn域名价格 万网域名代理 工信部域名备案系统 注册cn域名 息壤备案 unsplash tightvnc 免费ftp空间申请 大容量存储器 国外免费全能空间 柚子舍官网 网站木马检测工具 老左正传 可外链网盘 免费网页申请 彩虹云 独享主机 shuang12 linode支付宝 更多