强行终止线程,急,谢(200分)

  • 主题发起人 主题发起人 13156191019
  • 开始时间 开始时间
Inifinite不用定义,是时间无穷大的意思,如果将第二个参数设置为Inifinite,则表明只有当线程接收到mythread.Handle这个信号(一般用SetEvent给信号),线程才被激活。也可以输入整数,单位是毫秒,比如WaitForSingle(mythread.Handle, 30),那么30毫秒后即使没有接收到信号,线程也被激活。
 
还是不行,我用了WaitForSingle(mythread.Handle, INFINITE),一闪而过,没有什么作用,程序关闭后,还会在win2000的任务管理器中看到该进程,我[:(][:(],怎么办呢[:D]
请大家订阅邮件好吗
 
建议先看偶这篇文章学习一下再说:
http://www.csdn.net/develop/Read_Article.asp?Id=22466
 
mythread.Suspend;
//停止
mythread.Resume;
//启动
 
猛禽,我学习完你那篇文章了,好像没写完,依你看,我的线程终止后,在操作系统的残留部分怎么干掉呢
 
我看完了,原来根本就释放不了,只有用API或RTL(Runtime Library)进行多线程编程才可以立即停止线程并释放,谁有API或RTL(Runtime Library)进行多线程编程的例子
 
请教为什么要用WaitForSingleObject?用thread.waitfor不行吗?
 
Calling WaitForSingleObject(mythread.Handle, INFINITE) success, it means
mythread has been terminated. so the problem must be in your main application.
BTW: WaitForSingleObject returns WAIT_OBJECT_0, mythread has been released
from system, only mythread info struct has been left in system. call
CloseHandle(mythread.Handle), if no other processes use mythread, the thread
info struct will be released too. For details, refer Windowns Kernel Programming.
 
其实问题可能根本就不在线程
 
TO:weiwei81123,
mythread.Suspend;

mythread.Free;
运行到后一句,主程序停住不动了,怎么办[8D]
 
//try my code:
unit uthreadtest;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
type
TTestThread = class(TThread)
protected
procedure Execute;
override;
end;

procedure TTestThread.Execute;
begin
while truedo
;
end;

var
TestThread: TTestThread;
procedure TForm1.FormDestroy(Sender: TObject);
begin
TestThread.Suspend;
TestThread.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
TestThread := TTestThread.Create(false);
end;

end.
 
也许是线程被强制停止后但它所占的栈还留着,或者是由于线程申请的内存没被释放
 
•关闭主窗体时应注意:当线程正在运行时不允许关闭,强制关闭可能出现严重后果。
{创建线程}
type
TThreadRunMrp_Generate=Class(TThread)
QT_Handle:THandle;
ExitCode:DWord;
Private
Protected
Procedure Execute;Override;
end;

var ThreadRunMrp_Generate:TThreadRunMrp_Generate;
Procedure TThreadRunMrp_Generate.Execute;
begin
//
FreeOnTerminate:=True;
QT_Handle:=Test.Handle;
//关闭主窗体是就可以以此判断是否有线程在运行。
as_Mrp_GenerateForm.Run;
//线程所运行的过程;(程序自定义过程)
end;

//
procedure Tas_Mrp_GenerateForm.btnRunClick(Sender: TObject);//触发程序中的一个按钮事件
begin
//
ThreadRunMrp_Generate:=TThreadRunMrp_Generate.Create(False);
//创建线程;
end;

//停止线程
procedure Thread.ThreadKill;
begin
TerminateThread(QT_Handle,ExitCode);
end;

//触发线程停止(Test)
Test.ThreadKill;
if Test.FreeOnTerminate then
begin
//showmessage('OK');
Test.DoTerminate;
Test.Free;
end;
 
楼上是的程序调试过没有, 关注中...
 
不是多线程没有结束的问题,可能是什么地方没有释放资源(比如数据库连接等)
有下面一个最简单的强行结束线程的例子,强行结束后并没有报错,而且任务管理器中也没有了
unit Unit1;

interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TSleepThread = class (TThread)
protected
procedure Execute;override;
public
Constructor Create;
end;


TForm1 = class(TForm)
btnCreate: TButton;
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure btnCreateClick(Sender: TObject);
private
FThread:TSleepThread;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
{$R *.DFM}

{ TSleepThread }
constructor TSleepThread.Create;
begin

inherited Create(False);
FreeOnTerminate:=True;
end;

procedure TSleepThread.Execute;
begin

while not Terminateddo

begin

Sleep(1000000000);// 模拟长久执行
end;

end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin

if FThread<>nil then

begin

FThread.Terminate;
//FThread.WaitFor;
不要加这句,否则就是。。。。
end;

end;

procedure TForm1.btnCreateClick(Sender: TObject);
begin

if FThread=nil then

FThread:=TSleepThread.Create;
end;

 
这是俺用线程写的定时器,用的软退出。
TTimerThread = class(TThread)
private
FInterval : Integer;
FStopEvent: THandle;

FOnTimerTick : TNotifyEvent;
protected
procedure Execute;
override;
procedure Stop;
public
constructor Create;
destructor Destroy;
override;
property Interval: Integer read FInterval write FInterval;
property OnTimerTick: TNotifyEvent read FOnTimerTick write FOnTimerTick;
end;

constructor TTimerThread.Create();
begin
inherited Create(True);
FStopEvent := CreateEvent(nil, True, False, nil);
end;

// destroy thread
destructor TTimerThread.Destroy;
begin
Stop;
CloseHandle(FStopEvent);
inherited Destroy;
end;

// thread action
procedure TTimerThread.Execute;
var
Signaled: DWORD;
begin
repeat
Signaled := WaitForSingleObject(FStopEvent,FInterval);
// if event occurs, dispatch it
if (Signaled = WAIT_TIMEOUT and Assigned(FOnTimerTick)) then
FOnTimerTick(Self);

until Signaled <> WAIT_TIMEOUT;
end;

// stop thread
procedure TComThread.Stop;
// 软退出
begin
SetEvent(FStopEvent);
Sleep(0);
end;
 
mythread.terminate;
mythread.Free;
 
后退
顶部