假设我有一个 GUI 应用程序,它有一个在应用程序的生命周期内运行的后台线程。当我关闭应用程序时,我想干净地关闭任何这些后台线程。实际上,我经常运行一些线程来执行数据收集和处理活动,而无需挂起 GUI。
下面的例子演示了这个问题;也就是说,如果您取消后台工作人员,它会尝试在主线程上调用工作人员完成方法。如果不调用Application::DoEvents()
代码只会无限期地挂起,但我之前遇到过调用问题DoEvents
,我的直觉告诉我这是不好的做法。
所以问题是;当我的应用程序退出时,干净地关闭后台工作线程的正确方法是什么?
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
};