Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6031329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:10:03+00:00 2026-05-23T05:10:03+00:00

How can I properly stop a thread when an application is closing? I do

  • 0

How can I properly stop a thread when an application is closing?

I do this:

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if not Thread1.Finished
then
  begin
    Thread1.Terminate;
    Thread1.WaitFor();
  end;
end;

But on Thread1.WaitFor I have an error: “Thread Error: The handle is invalid (6).” If I do WaitForSingleObject(Thread1.Handle,infinite) instead of WaitFor all is ok.

Why if I use Thread.freeonterminate := false then WaitFor works good? Explain me please what I do wrong.
As I understand I need to use “if Assigned” instead of “if not Thread1.Finished”, right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:10:03+00:00Added an answer on May 23, 2026 at 5:10 am

    When you set FreeOnTerminate = True, the thread object automatically frees itself when it terminates. So, after it terminates, any further calls on that object are invalid. As soon as you call Terminate, you have to assume that the object no longer exists.

    If you need to do further operations on a thread after starting it, then don’t set FreeOnTerminate. Instead, free it manually after you’re really finished using it.

    The only time you would use Assigned is if you’re expecting the Thread1 variable to be nil. Do you ever assign Thread1 := nil? If not, then you shouldn’t expect it to have that value. As you should know, variables don’t suddenly change their values when you call methods on them. But if you’ve set FreeOnTerminate, then it’s incorrect to check the Finished property, too, because it might have already finished and freed itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope I can explain this properly. I'm using flashDevelop for an avatar creation
Can IDataError info be used properly in a winforms application? In the past I
I can't figure out why this list is not displaying properly in IE7. The
I can't for the life of me figure out how to do this properly.
Could someone please help explain why I can't get this to work? I properly
I use asp:Login control, user can login properly, but while checking user Profile information
Can someone provide a simple example of how to properly use Html.RadioButtonFor? Let's say
How can I check whether all JavaScript functions have loaded properly on a page?
How can I tell if my call to SendInput is working properly? I have
Short story: I can't make precompiled headers work properly with gcc -c option. Long

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.