응용 프로그램의 수명 동안 실행되는 백그라운드 스레드가있는 GUI 응용 프로그램이 있다고 가정합니다. 응용 프로그램을 닫을 때 이러한 백그라운드 스레드를 깨끗하게 닫고 싶습니다. 실제로는 GUI를 끊지 않고 데이터 수집 및 처리 작업을 수행하기 위해 몇 개의 스레드가 실행되는 경우가 종종 있습니다.application :: doevents를 호출하지 않고 백그라운드 작업자 스레드를 종료하려면 어떻게해야합니까?
아래 예제에서는 문제를 보여줍니다. 즉, 백그라운드 작업자를 취소하면 주 스레드에서 작업자 완료 메소드가 호출됩니다. Application::DoEvents()
에 대한 호출이 없으면 코드는 무기한으로 중지되지만, 전에 DoEvents
을 호출하는 문제가 발생했습니다. 내 직감은 나쁘다고합니다.
질문은 다음과 같습니다. 내 응용 프로그램이 종료되면 백그라운드 작업자 스레드를 완전히 종료하는 올바른 방법은 무엇입니까? BackgroundWorker에이 RunWorkerCompleted 이벤트가 발생하면
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
};
가능한 해결책을 찾았습니다. http://social.msdn.microsoft.com/Forums/nl/csharpgeneral/thread/14679d00-3d46-4de4-be87-e66c196f0638 –
[가능한 중복] (http : // stackoverflow .com/questions/4732737/how-to-stop-backgroundworker- 정확하게)'DoEvents'의 사용을지지하는 한 답변 (http://stackoverflow.com/a/7625188/15369) ... –
DoEvents에 대한 [일부 흥미로운 배경 정보] (http://blogs.msdn.com/b/jfoscoding/archive/2005/08/06/448560.aspx) ... –