[C ++,Windows窗体]如何使主线程等待被调用线程完成?

时间:2018-11-14 05:17:30

标签: multithreading c++-cli

我创建了2个按钮,一个用于启动新线程,另一个用于结束线程。新线程内部的实际计算涉及new []和delete [],因此我不直接中止线程,而是使用标志将其终止。结束delete []和保存结果可能需要一些时间,因此我希望主线程等待新线程结束。但是,尽管我尝试了一下,但我发现直到执行停止按钮的命令行后,新线程才会运行(尽管其ThreadState正在运行)。 System :: Threading :: Thread与我的工作原理完全不同。应该是这样吗?

#include "stdafx.h"

ref class Form1 : System::Windows::Forms::Form
{
public:
//define a thread name and a flag to terminate the thread
System::Threading::Thread^ th1;
static bool ITP1=0;

Form1(void)
{InitializeComponent();}

System::Windows::Forms::Button^ ButtonStart;
System::Windows::Forms::Button^ ButtonStop;
System::Windows::Forms::Label^ Label1;

void InitializeComponent(void)
{
    this->SuspendLayout();

    this->ButtonStart = gcnew System::Windows::Forms::Button();
    this->ButtonStart->Location = System::Drawing::Point(20, 20);
    this->ButtonStart->Click += gcnew System::EventHandler(this, &Form1::ButtonStart_Click);
    this->Controls->Add(this->ButtonStart);

    this->ButtonStop = gcnew System::Windows::Forms::Button();
    this->ButtonStop->Location = System::Drawing::Point(120, 20);
    this->ButtonStop->Click += gcnew System::EventHandler(this, &Form1::ButtonStop_Click);
    this->Controls->Add(this->ButtonStop);

    this->Label1 = gcnew System::Windows::Forms::Label();
    this->Label1->Location = System::Drawing::Point(20, 80);
    this->Controls->Add(this->Label1);

    this->ResumeLayout(false);
    }

void ThreadStart()
{
    for (int idx=0;idx<999999999;++idx)
    {
        if (ITP1) break;
    }
    this->Label1->Text = "finished";
    ITP1=0;
}

System::Void ButtonStart_Click(System::Object^ sender, System::EventArgs^ e)
{
    th1 = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this,&Form1::ThreadStart));
    th1->Start();
    this->Label1->Text = "running";
}

System::Void ButtonStop_Click(System::Object^ sender, System::EventArgs^ e)
{
    if (th1->ThreadState==System::Threading::ThreadState::Running)
    {
        //use the flag to stop the thread
        ITP1=1;
        //the wait method using while+sleep doesn't work
        while (th1->ThreadState==System::Threading::ThreadState::Running)   System::Threading::Thread::Sleep(1000);
        //replacing the wait method above with "th1->Join()" doesn't work either
    }
}
};

int main()
{
Form1^ A1 = gcnew Form1();
A1->ShowDialog();
return 0;
}

2 个答案:

答案 0 :(得分:1)

您必须在主线程中join()被调用的线程。然后,主线程将等待,直到被调用线程完成。

请参阅Join的文档以了解如何调用它。

答案 1 :(得分:0)

最后我找到了原因。它只是新线程中的“ this->”指针。删除它可以使一切正常。 我想这是因为Form允许一次仅对一个元素进行操作。我要求单击按钮以等待新线程,然后新线程尝试编辑其他表单元素。他们彼此等待结束并导致死循环。

相关问题