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
};