关于一个进程操作的问题。up有分 如果我创建了一个线程hthread:=CreateThread(nil,0,@DoFunc,nil,0,ThreadID);那这个线程什么时候结束呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Execute 执行完或者外部终止它 有个api函数可以终止它的,但需要这个线程的句柄 外部终止线程 : TerminateThread(TTheThread(TTestThread).Handle,0); // TTheThread(TTestThread).Free ; 啊,楼上的理解错了啊 TerminateThread(hthread,0);这个api我知道,hthread可以通过hthread:=CreateThread(nil,0,@DoFunc,nil,0,ThreadID);得到我不是说要怎么终结这个线程,而是问这个线程什么时候自动结束,返回个什么标志,参数之类的。而不要我手动终止。wjlsmail(计算机质子):Execute 操作是相对于Thread类吧, 我没有用delphi封装的Thread。我只是调用api而已。 wjlsmail(计算机质子):TTheThread 是个什么东西?我怎么查不到帮助? TTheThread :我自己定义的一个线程 TTheThread :我自己定义的一个线程类 楼上,我完全没有用到delphi封装的thread类。所以你的方法也不能实现我的目的。我目前是这样做的,我hthread:=CreateThread(nil,0,@DoFunc,nil,0,ThreadID);创建一个线程来执行Dofunc函数,当这个函数执行完以后,给一个变量赋值,比如给TStatus:=True,然后搞个Timer不断的判断这个TStatus的状态,为True时就把这个线程咔嚓掉~这样做实在是有蛮龌龊。不知哪位老大有好的方法? hthread:=CreateThread(nil,0,@DoFunc,nil,0,ThreadID);结束 : DoFunc 执行完 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; function MyThreadFunc(P : pointer) : Longint;stdcall;implementation{$R *.dfm}function MyThreadFunc(P : pointer) : Longint;stdcall;var i:integer; S:string;begin for i:=0 to 100000 do S:=Inttostr(i); MessageBox(Application.Handle,'执行完成','Note',Mb_Ok) ;end;procedure TForm1.Button1Click(Sender: TObject);var HThread : THandle ; ThreadID : DWord ;begin HThread := CreateThread(Nil,0,@MyThreadFunc,Nil,0,ThreadID) ;end;end. 在线程内部用ExitThread(exitcode);外部用GetExitCodeThread(hthread)来取得exitcode 一个线程的结束有二种方式:自动退出: 是指在线程完成其Execute方法后直接返回,如果此时线程对象的FreeOnTerminate属性设为True,则线程将自动删除,并释放线程对象所占用的资源;受控退出: 由进程或其他线程调用所需结束的线程对象的Terminate方法,并设置线程对象的Terminate属性为True. chart 的简单问题 文件流的一个小问题 急求救:idtcpserver 如何和C 程序地客户端通信 为什么我的DBGRID只显示一行? IntraWeb怎样访问其它窗体? 无聊5.1大派分 送出510分~ 之三 我的程序怎么了,老是报运行时错误?是不是和我的用的控件有关系啊。以前是好的。up有分! 请问那里有用delphi写的机房管理系统的源代码? 用DELPHI调用的SQL数据中,我想让有些数据变颜色(如超出查询范围就变色),具体该如何操作啊? 怎样删除自己的贴子? 关于打印机的消息处理 一个SQL查询的问题,帮帮手,急啊!
或者外部终止它
TTheThread(TTestThread).Free ;
TerminateThread(hthread,0);这个api我知道,hthread可以通过
hthread:=CreateThread(nil,0,@DoFunc,nil,0,ThreadID);得到我不是说要怎么终结这个线程,而是问这个线程什么时候自动结束,
返回个什么标志,参数之类的。而不要我手动终止。
wjlsmail(计算机质子):Execute 操作是相对于Thread类吧,
我没有用delphi封装的Thread。我只是调用api而已。
wjlsmail(计算机质子):TTheThread 是个什么东西?我怎么查不到帮助?
的目的。我目前是这样做的,我
hthread:=CreateThread(nil,0,@DoFunc,nil,0,ThreadID);
创建一个线程来执行Dofunc函数,当这个函数执行完以后,
给一个变量赋值,比如给TStatus:=True,然后搞个Timer不断的
判断这个TStatus的状态,为True时就把这个线程咔嚓掉~这样做实在是有蛮龌龊。不知哪位老大有好的方法?
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
function MyThreadFunc(P : pointer) : Longint;stdcall;implementation{$R *.dfm}function MyThreadFunc(P : pointer) : Longint;stdcall;
var
i:integer;
S:string;
begin
for i:=0 to 100000 do
S:=Inttostr(i);
MessageBox(Application.Handle,'执行完成','Note',Mb_Ok) ;
end;procedure TForm1.Button1Click(Sender: TObject);
var
HThread : THandle ;
ThreadID : DWord ;
begin
HThread := CreateThread(Nil,0,@MyThreadFunc,Nil,0,ThreadID) ;
end;end.
外部用GetExitCodeThread(hthread)来取得exitcode
自动退出:
是指在线程完成其Execute方法后直接返回,如果此时线程对象的FreeOnTerminate属性设为True,则线程将自动删除,并释放线程对象所占用的资源;受控退出:
由进程或其他线程调用所需结束的线程对象的Terminate方法,并设置线程对象的Terminate属性为True.