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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:57:28+00:00 2026-06-07T11:57:28+00:00

Suppose I have a GUI application which has a background thread which runs for

  • 0

Suppose I have a GUI application which has a background thread which runs for the lifetime of the application. When I close the application I want to close off any of these background threads cleanly. In reality I often have a few threads running to perform data collection and processing activities without hanging up the GUI.

The example below demonstrates the problem; namely that if you cancel the background worker, it tries to call the worker complete method on the main thread. Without a call to Application::DoEvents() the code will just hang indefinitely, but I’ve run into issues with calling DoEvents before and my gut tells me it’s bad practice.

So the question is; When my application exits, what’s the right way to cleanly shutdown the background worker thread?

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
///          'Resource File Name' property for the managed resource compiler tool
///          associated with all .resx files this class depends on.  Otherwise,
///          the designers will not be able to interact properly with localized
///          resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
private: System::ComponentModel::BackgroundWorker^  backgroundWorker1;

public:
    Form1(void)
    {
        InitializeComponent();

        // 
        // backgroundWorker1
        // 
        this->backgroundWorker1 = (gcnew System::ComponentModel::BackgroundWorker());
        this->backgroundWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &Form1::backgroundWorker1_DoWork);
        this->backgroundWorker1->RunWorkerCompleted += gcnew System::ComponentModel::RunWorkerCompletedEventHandler(this, &Form1::backgroundWorker1_RunWorkerCompleted);
        this->backgroundWorker1->ProgressChanged += gcnew System::ComponentModel::ProgressChangedEventHandler(this, &Form1::backgroundWorker1_ProgressChanged);
        this->backgroundWorker1->WorkerReportsProgress = true;
        this->backgroundWorker1->WorkerSupportsCancellation = true;

        //
        //TODO: Add the constructor code here
        //
        backgroundWorker1->RunWorkerAsync();
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Form1()
    {
        if( backgroundWorker1 != nullptr )
        {
            backgroundWorker1->CancelAsync();
        }

        while( backgroundWorker1->IsBusy == true )
        {
            System::Diagnostics::Debug::WriteLine("Waiting for background worker to exit..");
            System::Threading::Thread::Sleep(1000);
            // Application::DoEvents(); <-- Don't want to do this but what are the alternatives?
        }

        System::Diagnostics::Debug::WriteLine("Form1 destructor complete!");
    }

private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
             while( backgroundWorker1->CancellationPending == false )
             {
                 System::Diagnostics::Debug::WriteLine("Working..");
                 System::Threading::Thread::Sleep(1000);
             }
         }

private: System::Void backgroundWorker1_ProgressChanged(System::Object^  sender, System::ComponentModel::ProgressChangedEventArgs^  e) {
         }

private: System::Void backgroundWorker1_RunWorkerCompleted(System::Object^  sender, System::ComponentModel::RunWorkerCompletedEventArgs^  e)
         {
            System::Diagnostics::Debug::WriteLine("Exiting..");
            System::Threading::Thread::Sleep(1000);

         }

private:
    /// <summary>
    /// Required designer variable.
    /// </summary>
    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
        this->SuspendLayout();
        // 
        // Form1
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(443, 343);
        this->Name = L"Form1";
        this->Text = L"Form1";
        this->ResumeLayout(false);

    }
#pragma endregion

};
  • 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-06-07T11:57:30+00:00Added an answer on June 7, 2026 at 11:57 am

    When BackgroundWorker fires the RunWorkerCompleted event, it does so on the main GUI thread, which is the one that the destructor is running on. When you call DoEvents, it gives the framework a chance to run other tasks that are waiting to run on that thread, including the RunWorkerCompleted event.

    I added Thread::CurrentThread->ManagedThreadId to your debug messages (and uncommented Application::DoEvents()), and here’s what I got:

    Starting background worker from thread 1
    Working on thread 3..
    Working on thread 3..
    Waiting on thread 1 for background worker to exit..
    Exiting on thread 1..
    Form1 destructor complete!
    

    So, if you don’t want to use Application::DoEvents(), I’d use a different mechanism to tell the UI thread that the worker has exited. A ManualResetEvent will do the trick nicely here.

    private:
        ManualResetEvent^ mre;
    
    public:
        Form1(void)
        {
            InitializeComponent();
    
            this->backgroundWorker1 = (gcnew System::ComponentModel::BackgroundWorker());
            this->backgroundWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &Form1::backgroundWorker1_DoWork);
            // Don't use this->backgroundWorker1->RunWorkerCompleted
            this->backgroundWorker1->ProgressChanged += gcnew System::ComponentModel::ProgressChangedEventHandler(this, &Form1::backgroundWorker1_ProgressChanged);
            this->backgroundWorker1->WorkerReportsProgress = true;
            this->backgroundWorker1->WorkerSupportsCancellation = true;
    
            this->mre = gcnew ManualResetEvent(false);
    
            System::Diagnostics::Debug::WriteLine("Starting background worker from thread {0}", Thread::CurrentThread->ManagedThreadId);
            backgroundWorker1->RunWorkerAsync();
        }
    
    protected:
        ~Form1()
        {
            if( backgroundWorker1 != nullptr )
            {
                backgroundWorker1->CancelAsync();
            }
    
            System::Diagnostics::Debug::WriteLine("Waiting on thread {0} for background worker to exit..", Thread::CurrentThread->ManagedThreadId);
            this->mre->WaitOne();
    
            System::Diagnostics::Debug::WriteLine("Form1 destructor complete!");
        }
    
    private:
        System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e)
            {
                try
                {
                    while( backgroundWorker1->CancellationPending == false )
                    {
                        System::Diagnostics::Debug::WriteLine("Working on thread {0}..", Thread::CurrentThread->ManagedThreadId);
                        System::Threading::Thread::Sleep(1000);
                    }
                }
                finally
                {
                    this->mre->Set();
                }
            }
    

    Now, all that said, you may want to consider using a regular Thread here, rather than a BackgroundWorker. By firing the ‘completed’ and ‘progress’ events onto the UI thread, I think it’s more geared toward tasks that are too long to do directly on the UI thread, but where the result will be visible in the UI a few seconds later. (Firing the events on the UI thread makes it easy to update UI controls with the result of the operation.) For what you’ve shown here, something that’s running the entire time that the form is open, you’re not getting a big benefit from BackgroundWorker, so you could just create a thread and do it yourself.

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

Sidebar

Related Questions

Suppose I have a GUI-only application that runs on Windows and I'd like to
I have a GUI application, which creates a QProcess inside, catches its output and
Suppose you have a client/server application, say a webserver component and a qt gui.
Suppose I have a design like this: Object GUI has two objects: object aManager
Suppose we have the name written in any none-latin letters - languages, like Arabic,
Suppose I have an application that access data resident in a central DB server
I want to use Qt to create a simple GUI application that can play
I have a command-line application written in C++ and built with gcc/make that runs
At the moment I have an existing application which basically consists of a desktop
I have developed a GUI for a random application using WPF. I have a

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.