朋友!
如果我使用一个启动了嵌套线程的线程,那么终止应用程序(违反内存)时会遇到问题。如果最深的嵌套线程卡住(等待超时),则应用程序终止时会发生错误。在其他情况下,问题不会出现。我做错了什么?请帮帮我。
type tSync = class(tThread) procedure Execute; override; end; var Sync : tSync; procedure tMainForm.FormCreate(Sender: TObject); begin Sync:=tSync.Create; end; procedure tMainForm.FormDestroy(Sender: TObject); begin Sync.Free; end; procedure tSync.Execute; var TCP:tIdTCPClient; begin while not(Terminated) do begin TCP:=tIdTCPClient.Create; try {any code} TCP.Commect; { if I close the mainform when it stuck here (waiting for a timeout), } { an error of memory violation falls out in main application thread } {any code} except {error handler} end; TCP.Free; Sleep(10); end; end;
据我所知,tSync.Free不会等待TCP.Connect完成并自行关闭。TCP.Connection在超时结束后无处返回。
Delphi社区10.4版,Windows 10