我给线程的OnTerminate指定了事件,可是线程结束的时候却没有调用我指定的事件;(100分)

  • 主题发起人 主题发起人 makes
  • 开始时间 开始时间
M

makes

Unregistered / Unconfirmed
GUEST, unregistred user!
我给线程的OnTerminate指定了事件,可是线程结束的时候却没有调用我指定的事件;
而且我做了~THREAD的事件,也没有被调用;
我的线程设定了FreeOnTerminate的属性的;
我是调用Terminate正常的让线程退出循环的;
请高手赐教怎么能让线程结束的时候做一见指定的事情;
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure abc(Sender: TObject);
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
uses Unit2;
{$R *.DFM}
procedure TForm1.abc(Sender: TObject);
begin
ShowMessage('wo shi xiaobenben');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
dfd: TMyThread;
begin
dfd := TMyThread.Create(true);
dfd.OnTerminate := abc;
dfd.Resume;
end;

end.
 
//我的线程设定了FreeOnTerminate的属性的;
//我是调用Terminate正常的让线程退出循环的;
既然FreeOnTerminate的属性为True,就不用调用Terminate。
 
//既然FreeOnTerminate的属性为True,就不用调用Terminate。
不同意
 
Sorry,是不用调用Free方法了。
 
我以前学习线程时写的一个例子,你看看吧
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,FileCtrl;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
ComboBox1: TComboBox;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
procedure ThreadDone(Sender: Tobject);
public

{ Public declarations }
end;

//¶¨ÒåÏß³Ì
TFooThread = class(TThread)
protected
procedure execute;override;
end;

var
Form1: TForm1;

implementation
{$R *.DFM}
const MaxSize=128;
var
NextNumber:integer=0;
do
neFlags:integer=0;
GlobalArray:array[1..MaxSize] of integer;
//CS:TRTLCriticalSection;
hMutex:Thandle=0;

{ TFooThread }
function GetNextNumber: integer;
begin
Result:=NextNumber;
inc(nextNumber);
end;

procedure TFooThread.execute;
var
i:integer;
begin
onTerminate:=Form1.ThreadDone;
//EnterCriticalSection(CS);
if WaitForSingleObject(hMutex,INFINITE)=WAIT_OBJECT_0 then
begin

for i :=1 to MaxSize do
begin
Globalarray:=GetNextNumber;
// sleep(5);
end;
//LeaveCriticalSection(cs);

end;

ReleaseMutex(hMutex);
end;

procedure TForm1.ThreadDone(Sender: Tobject);
var
i,stop:integer;
begin
inc(doneflags);
stop:=strtoint(comboBox1.text);
ifdo
neflags=stop then
begin
listbox1.Items.Clear ;
for i := 1 to MaxSizedo
Listbox1.Items.Add(inttostr(GlobalArray));
// DeleteCriticalSection(cs);
CloseHandle(hMutex);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
hMutex:=CreateMutex(nil,false,nil);
// InitializeCriticalSection(cs);
do
neFlags:=0;
nextNumber:=0;
TFooThread.Create(False);
TFooThread.Create(False);
TFooThread.Create(False);
// showmessage(trim(datetimetostr(now)));
end;

procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
fname:string;
begin
chdir('e:/temp');
if (not directoryExists(datetostr(now))) then
mkdir(datetostr(now));//½¨Á¢ÈÕÆÚĿ¼
fname:=datetostr(now)+'_0.txt';
chdir(datetostr(now));
i:=0;
while (FileExists(fname))do
begin
i:=i+1;
fname:=datetostr(now)+'_'+IntToStr(i)+'.txt';
end;


listbox1.Items.SaveToFile(fname);

end;

end.
 
我怀疑你的线程没有正确的结束,我看了VCL代码,应该是没问题的,
你好好检查一下你的线程里的逻辑问题
 
我试过了,没有遇到楼主的问题,可能是楼主的线程写的有问题。
 
后退
顶部