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 6025483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:18:27+00:00 2026-05-23T04:18:27+00:00

Once, during testing, my C++Builder / Delphi application raised an uncaught exception in a

  • 0

Once, during testing, my C++Builder / Delphi application raised an uncaught exception in a background worker thread. EurekaLog caught the exception and sent an error report, as I expected, and everything appeared to be okay.

However, when I closed the application’s main window, something remained running in the background, because the application was still listed in the task manager (and still had resources open).

I’ve tried to duplicate this problem, by deliberately introducing various bugs in the background worker thread, but I can’t.

What could cause a thread and application to remain running like this, even after the main window has closed (and, presumably, PostQuitMessage has been called)?

How can I ensure that the application always cleanly shuts down?

  • 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-23T04:18:27+00:00Added an answer on May 23, 2026 at 4:18 am

    The first rule is that threads main executor methods should be written so that they can be signalled and shut down properly, and the second rule is that you shouldn’t just shut down your app’s main thread first, and then hope that the other threads shut down in their own time, to be safe, you should signal all the background threads to stop, wait for that shutdown to complete, and THEN shutdown your main thread. A minimal THREAD example:

    procedure TMyThread.Execute;
    begin
       Init;
       while not Terminated do
          OneWorkItem; // inside OneWorkItem, you  ALSO need to check for Terminated
    end;
    

    A minimal main form/main-thread example:

       procedure TMyMainForm.CheckAndShutdown;
       begin
         if FPendingShutdownFlag then 
           if AllBackgroundThreadsTerminated then 
                 Self.Close;
       end;
    

    You could set FPendingShutdownFlag and have the function above called from the application idle processing loop. When you have the user click the main form FormClose, if AllBackgroundThreadsTerminated returns false, set CanClose to false, and set the FPendingShutdownFlag := true instead.

    If you make an endless loop (while true), the application does not shut down cleanly, even if it looks like that to you. Somehow, the application is terminated, and the running threads may just suddenly go away quietly, or they may deadlock or otherwise fail on you, as they may be using resources in thread 2 that you are busy freeing in thread 1.

    You may have one or more intentional race conditions because you might not have written your thread execute method to be interruptable, or you may start the closing of the main application thread and the VCL and its objects, before you are sure that your background threads are completely shut down.

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

Sidebar

Related Questions

I want to show the splash screen only once during the Application life cycle.
Application_Start and Application_End are called only once during the lifetime of the application domain
During a complicated update I might prefer to display all the changes at once.
I'm using the python api and have created a few orphaned blobs during testing.
I'm testing an android application with a long running service. I'm using Eclipse and
I am working with multiple databases in a PHP/MySQL application. I have development, testing,
Probably everyone ran into this problem at least once during development: while(/*some condition here
During the life of my web application I know all of my services and
Hi: I have a multi thread Java application. There are many temporary objects. -XX:MaxTenuringThreshold=1
Once upon a time, I dabbled in programming Homebrew for the Nintendo DS. During

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.