终止多个线程的问题(50分)

  • 主题发起人 主题发起人 lunyx_allen
  • 开始时间 开始时间
L

lunyx_allen

Unregistered / Unconfirmed
GUEST, unregistred user!
我用了循环建立多个线程,如下:
for i:=0 to 100 do
Tthread.create(false);

现在我要把这些线程统统都终止执行,该用什么代码?
 
正确方法是让线程运行完后,自动退出,可以在循环体内放入判断语句,(在execute中检查terminated),
强制关线程可以用 terminate(handle,0),但这个函数在WINDOWS NT下不能清除栈。
 
关键是怎么找出那么多线程的句柄,然后 终止它啊
 
handle:=Tthread.create(false);
 
如果直接用
handle:=Tthread.create(false);
handle.terminate的话,终止一个而已吧。问题就是要找出所有这些线程的句饼啊,我循环建立了好多线程的
 
你不会存列表啊
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Contnrs, Dialogs, StdCtrls,unit2;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
OS : Tobjectlist ;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
i : Integer ;
MyThread : TMyThread ;
begin
if OS = nil then
begin
OS := TObjectList.Create ;
for i:=0 to 10 do
OS.Add(TMyThread.create(false)) ;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
i : Integer ;
begin
if OS <>nil then
begin
for i:=0 to 10 do
TerminateThread( (OS.Items as TMythread).Handle,0) ;
OS.Clear ;
FreeAndNil(OS);
end;
end;

end.



unit Unit2;

interface

uses
Classes,SysUtils;

type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;

implementation

uses Unit1;

{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,

Synchronize(UpdateCaption);

and UpdateCaption could look like,

procedure TMyThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }

{ TMyThread }

procedure TMyThread.Execute;
begin
{ Place thread code here }
sleep(1000000);
end;



end.
 
可先建一全局变量:
hStopEvent : HANDLE ;
并在建立线程前执行下面的语句:
hStopEvent := CreateEvent( nil , false , false ,nil);
在线程中检测此信号灯是否点亮,点亮则退出线程例如:
if ( WaitForSingleObject( hStopEvent , 0 ) = WAIT_OBJECT_0 ) then
begin
//信号灯亮了,退出线程
end;
当需要停止全部线程时,只需要点亮信号灯即可以了,即:
SetEvent( hStopEvent );

//完!Good luck
 
angelsoft 的代码,可能实现线程关闭的功能,但是代码更好像在演示事件对象怎么用的。

但是有二个问题,楼主要注意的:
一、那个线程的设计好像只是为了退出而设计的,
想要关闭线程时,必须保证线程已经执行到
if ( WaitForSingleObject( hStopEvent , 0 ) = WAIT_OBJECT_0 ) then
这句话,处于等待状态,
因为一般情况下,不知道什么时候要将线程全部关闭,这是很难保证的。其次线程运行到这时,线程就处于等待状态,什么也做不了了。就在等关闭操作。也就是说你的线程什么事也做不了了。
二、第二个参数还有点问题的。下面摘自MSDN中:
DWORD WaitForSingleObject(
HANDLE hHandle, // handle to object
DWORD dwMilliseconds // time-out interval
);
Parameters
hHandle
[in] Handle to the object. For a list of the object types whose handles can be specified, see the following Remarks section.
If this handle is closed while the wait is still pending, the function's behavior is undefined.

Windows NT/2000/XP: The handle must have SYNCHRONIZE access. For more information, see Standard Access Rights.

dwMilliseconds
[in] Specifies the time-out interval, in milliseconds. The function returns if the interval elapses, even if the object's state is nonsignaled. If dwMilliseconds is zero, the function tests the object's state and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses.
如果设为0,线程不会等待事件对象的。
改为:if ( WaitForSingleObject( hStopEvent , INFINITE
) = WAIT_OBJECT_0 ) then


最后,别忘记
CloseHandle(hStopEvent);
 
谢谢各位同仁,祝大家新年快乐,恭喜发财,步步高升
 
后退
顶部