[SOLVED]It takes long time to exit a App if it started threads lazarus / 21-General ---------------------------------------------------------------------- kinlion · 2022-12-17 03:41 Hi, all, I start a thread in  GUI app. When I close the app,  it takes a little long time, say about 5 seconds, to exits. I'm sure the thread has terminated and freed before the app closing. I guess the application has to do some final clean about thread. However, since I can ensure the thread has terminated and freed, I wonder is there anyway to short the time to exit? Win10-x64 + Lazarus 2.2.4 + FPC 3.2.2 best regards. ---------------------------------------------------------------------- Mr.Madguy · 2022-12-17 04:53 Use WaitFor to check if thread is terminated or not. Most likely it isn't and it's terminated by timeout. The best way to handle thread: thread is terminated by exiting it's Execute procedure, so check Terminated property often enough and exit if it's set. If you need to suspend thread - do it via event object and set it, if you need to terminate thread. ---------------------------------------------------------------------- jamie · 2022-12-17 10:54 Your thread code should be checking the terminate flag in its loop. When you shut down you should be calling the thread's terminate method which will set things in motion and your thread should be monitoring this.  If you wait for the OS to do it then you could be leaving a thread exiting in a bad spot. ---------------------------------------------------------------------- alpine · 2022-12-17 11:37 Quote from: kinlion on December 17, 2022, 04:41:44 amHi, all, I start a thread in  GUI app. When I close the app,  it takes a little long time, say about 5 seconds, to exits. I'm sure the thread has terminated and freed before the app closing. What do you mean with 'terminated'? TThread.Terminate just raises the Terminated flag and it is the TTread.Execute responsibility to check it and complete its job. if you just do: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   SomeThread.Terminate;  SomeThread.Free;You're actually freeing a live thread. At least there should be SomeThread.WaitFor in between. ---------------------------------------------------------------------- Thaddy · 2022-12-17 12:31 Yes, at least:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   SomeThread.Terminate;  SomeThread.Waitfor;  SomeThread.Free; ---------------------------------------------------------------------- kinlion · 2022-12-23 08:59 Yes, manully call Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i AThread.Terminate;AThread.Waitfor;AThread.Free; in FormDestroy seems faster. Thanks to all