Recent

Author Topic: [SOLVED]It takes long time to exit a App if it started threads  (Read 1121 times)

kinlion

  • Jr. Member
  • **
  • Posts: 82
  • I Love Lazarus
[SOLVED]It takes long time to exit a App if it started threads
« on: December 17, 2022, 04:41:44 am »
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.
« Last Edit: December 23, 2022, 10:00:11 am by kinlion »

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 881
Re: It takes long time to exit a App if it started threads
« Reply #1 on: December 17, 2022, 05:53:04 am »
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.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

jamie

  • Hero Member
  • *****
  • Posts: 7610
Re: It takes long time to exit a App if it started threads
« Reply #2 on: December 17, 2022, 11:54:45 am »
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.
The only true wisdom is knowing you know nothing

alpine

  • Hero Member
  • *****
  • Posts: 1412
Re: It takes long time to exit a App if it started threads
« Reply #3 on: December 17, 2022, 12:37:28 pm »
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.
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][+][-]
  1.   SomeThread.Terminate;
  2.   SomeThread.Free;
You're actually freeing a live thread. At least there should be SomeThread.WaitFor in between.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 18797
  • Glad to be alive.
Re: It takes long time to exit a App if it started threads
« Reply #4 on: December 17, 2022, 01:31:49 pm »
Yes, at least:
Code: Pascal  [Select][+][-]
  1.   SomeThread.Terminate;
  2.   SomeThread.Waitfor;
  3.   SomeThread.Free;
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

kinlion

  • Jr. Member
  • **
  • Posts: 82
  • I Love Lazarus
Re: It takes long time to exit a App if it started threads
« Reply #5 on: December 23, 2022, 09:59:50 am »
Yes, manully call
Code: Pascal  [Select][+][-]
  1. AThread.Terminate;
  2. AThread.Waitfor;
  3. AThread.Free;
  4.  
in FormDestroy seems faster.

Thanks to all  :)

 

TinyPortal © 2005-2018