gpt4 book ai didi

multithreading - Delphi:应用程序退出时杀死线程?

转载 作者:行者123 更新时间:2023-12-03 14:52:23 29 4
gpt4 key购买 nike

背景:我需要检查一组网络驱动器或远程计算机是否可用。由于每个 DirectoryExists() 需要很长时间才能出现潜在的超时,因此我在单独的线程中执行检查。当某些检查仍在运行时,最终用户可能会关闭应用程序。由于 DirectoryExists() 阻塞,我没有机会使用经典的 while not Termulated 方法。

procedure TMyThread.Execute;
begin
AExists := DirectoryExists(AFilepath);
end;

问题1:应用程序退出时某些线程仍在运行是否是一个问题? Windows 会简单地在我之后进行整理吗?在 IDE 内部,我收到未释放对象的通知,但在 IDE 外部,它看起来很平静。

问题 2:是否可以使用 TerminateThread 终止此类简单线程,或者在这种情况下这可能有害吗?

问题3:我通常在OnTerminate()事件中从线程中获取结果,然后让线程FreeOnTerminate。如果我想自己释放它们,我应该什么时候做?我可以在 OnTerminate 事件中释放线程吗?或者这是否有点太早了?如果不使用 OnTerminate,线程如何通知我它已完成?

最佳答案

Is it a problem that some threads are still running when the application quits?

可能是的。这取决于您的代码在 DirectoryExists() 退出后执行的操作。您最终可能会尝试访问不再存在的东西。

Will Windows simply tidy up after me and that's it?

为了确保正确清理所有内容,您有责任终止自己的线程。当VCL主线程运行完毕后,它将调用ExitProcess(),这将强制终止任何仍在运行的辅助线程,这将不允许它们自行清理,或通知任何已加载的线程。它们正在从线程中分离的 DLL。

Is it possible to terminate such simple threads with TerminateThread or is this potentially harmful in THIS case?

TerminateThread() 始终具有潜在危害。切勿使用它。

I usually take the results from the Threads in OnTerminate() event and let the threads FreeOnTerminate afterwards.

如果主消息循环在线程终止之前退出,那么这将不起作用。默认情况下,TThread.OnTerminate 事件是通过调用 TThread.Synchronize() 触发的。一旦主消息循环停止运行,将不会有任何东西来处理待处理的 Synchronize() 请求,除非您在应用程序退出时运行自己的循环来调用 RTL 的 CheckSynchronize() 过程,直到所有线程完全终止。

if I wanted to free them myself, when should I do it?

在您的应用想要退出之前。

Can I free a thread in its OnTerminate event

没有。

or is this a tiny bit too early?

因为在同一对象触发的事件中释放对象总是不安全的。事件处理程序退出后,RTL 仍然需要访问该对象。

话虽如此,由于您没有一种干净的方法来安全地终止线程,我建议不要在线程仍在运行时允许您的应用程序退出。当用户请求退出应用程序时,检查是否有线程正在运行,如果有,则向用户显示繁忙的 UI,等待所有线程终止,然后退出应用程序。

例如:

constructor TMyThread.Create(...);
begin
inherited Create(False);
FreeOnTerminate := True;
...
end;

procedure TMyThread.Execute;
begin
...
if Terminated then Exit;
AExists := DirectoryExists(AFilepath);
if Terminated then Exit;
...
end;

type
TMainForm = class(TForm)
...
procedure FormClose(Sender: TObject; var Action: TCloseAction);
...
private
ThreadsRunning: Integer;
procedure StartAThread;
procedure ThreadTerminated(Sender: TObject);
...
end;

...

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if ThreadsRunning = 0 then Exit;

// signal threads to terminate themselves...

if CheckWin32Version(6) then
ShutdownBlockReasonCreate(Handle, 'Waiting for Threads to Terminate');

try
// display busy UI to user ...

repeat
case MsgWaitForMultipleObjects(1, System.Classes.SyncEvent, False, INFINITE, QS_ALLINPUT) of
WAIT_OBJECT_0 : CheckSynchronize;
WAIT_OBJECT_0+1 : Application.ProcessMessages;
WAIT_FAILED : RaiseLastOSError;
end;
until ThreadsRunning = 0;

// hide busy UI ...
finally
if CheckWin32Version(6) then
ShutdownBlockReasonDestroy(Handle);
end;
end;

procedure TMainForm.StartAThread;
var
Thread: TMyThread;
begin
Thread := TMyThread.Create(...);
Thread.OnTerminate := ThreadTerminated;
Thread.Start;
Inc(ThreadsRunning);
end;

procedure TMainForm.ThreadTerminated(Sender: TObject);
begin
Dec(ThreadsRunning);
...
end;

或者:

type
TMainForm = class(TForm)
...
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
...
private
ThreadsRunning: Integer;
WaitingForClose: Boolean;
procedure StartAThread;
procedure ThreadTerminated(Sender: TObject);
...
end;

...

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := (ThreadsRunning = 0);
if CanClose or WaitingForClose then Exit;

// signal threads to terminate themselves...

WaitingForClose := True;

// display busy UI to user ...

if CheckWin32Version(6) then
ShutdownBlockReasonCreate(Handle, 'Waiting for Threads to Terminate');
end;

procedure TMainForm.StartAThread;
var
Thread: TMyThread;
begin
Thread := TMyThread.Create(...);
Thread.OnTerminate := ThreadTerminated;
Thread.Start;
Inc(ThreadsRunning);
end;

procedure TMainForm.ThreadTerminated(Sender: TObject);
begin
Dec(ThreadsRunning);

...

if WaitingForClose and (ThreadsRunning = 0) then
begin
WaitingForClose := False;

// hide busy UI ...

if CheckWin32Version(6) then
ShutdownBlockReasonDestroy(Handle);

Close;
end;
end;

关于multithreading - Delphi:应用程序退出时杀死线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49541543/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com