代码说明问题:子线程定义:// 状态发生了变化的Event
TStatusEvent = procedure of object;TMyThread = class(TThread)
private
FOnStatus: TStatusEvent;
protected
procedure Execute; override;
public
property OnStatus read FOnStatus write FOnStatus;
end;procedure TMyThread.Execute;
begin
...
if Assigned(FOnStatus) then FOnStatus; // 调用主线程的函数
end;// 主线程如下:TMyApp = class(TForm)
...
protected
procedure DoStatus;end;// 影响线程状态的变化
procedure TMyApp.DoStatus;
begin
processing := true;
....
processing := false;
end;procedure TMyApp.Exit;
TStatusEvent = procedure of object;TMyThread = class(TThread)
private
FOnStatus: TStatusEvent;
protected
procedure Execute; override;
public
property OnStatus read FOnStatus write FOnStatus;
end;procedure TMyThread.Execute;
begin
...
if Assigned(FOnStatus) then FOnStatus; // 调用主线程的函数
end;// 主线程如下:TMyApp = class(TForm)
...
protected
procedure DoStatus;end;// 影响线程状态的变化
procedure TMyApp.DoStatus;
begin
processing := true;
....
processing := false;
end;procedure TMyApp.Exit;
----------------------
子线程定义:// 定义一个状态EventTStatusEvent = procedure of object;
// 子线程TMyThread = class(TThread)
private
FOnStatus: TStatusEvent; // Event函数指针
protected
procedure Execute; override;
public
property OnStatus read FOnStatus write FOnStatus;
end;procedure TMyThread.Execute;
begin
...
if Assigned(FOnStatus) then FOnStatus; // 调用主线程的函数
end;// 主线程如下:TMyApp = class(TForm)
protected
MyThread: TMyThread; procedure DoStatus; // 响应子线程的状态
procedure Exit; // 退出
end;// 影响线程状态的变化
procedure TMyApp.DoStatus;
begin
processing := true;
....
processing := false;
end;// 退出程序,在退出前希望等待线程处理完毕
procedure TMyApp.Exit;
begin
MyThread.Terminate;
while processing do; // 等待DoStatus的结束,但是这里会导致子线程也被锁住了,
// 是不是子线程调用主线程的函数时,是在主线程的时间片中执行的?
end;输出调试信息得知,运行顺序如下:processing = true
call Exit;
死了
------
谁能告诉我答案
分不够另外开帖,俺的帖子向来是100分,这次不小心误操作。
call Exit;
??什么乱乱的
procedure TMyApp.DoStatus;
begin
processing := true;
OutputDebugString(PChar('processing = true')); // .......
....
OutputDebugString(PChar('processing = false')); // .......
processing := false;
end;// 退出程序,在退出前希望等待线程处理完毕
procedure TMyApp.Exit;
begin
Outputdebugstring(PChar('call Exit')); // ........
MyThread.Terminate;
while processing do; // 等待DoStatus的结束,但是这里会导致子线程也被锁住了,
// 是不是子线程调用主线程的函数时,是在主线程的时间片中执行的?
end;