首页 > 解决方案 > 使用 ref 类形式 c++ 中的线程进行并行工作

问题描述

我目前在 HMI 工作。我必须发送一条消息并等待回复。但是如果我使用 while 循环来等待响应,我的 HMI 会停止交互,等待 while 循环结束。

我尝试使用线程,并尝试在 HMI ref 类中创建一个线程函数来执行包含 while 循环的函数。但我不知道该怎么做。你能帮我解决这个问题,还是给我另一种方法来进行并行工作?谢谢

这是包含另一个类的while循环的代码

bool Robot::waitArrival(char* at)
{
    char _status[256];
    strcpy(_status, "Going to ");
    strcat(_status, static_cast<char*>(at));
        do
    {
        this->handUpdates->lock();
        strcpy(this->status, this->handUpdates->getStatus());
        this->handUpdates->unlock();

        ArUtil::sleep(1000); //1s
    } while (strcmp(this->status, _status) == 0);

    strcpy(_status, "Arrived at ");
    strcat(_status, static_cast<char*>(at));

    if (strcmp(this->status, _status) == 0)
        return true;
    else
        return false;
}

这是我的人机界面。通过点击button3我调用Controller ref类中的ctr_main()函数,ctr_main()函数调用waitArrival()函数,

    private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
        Thread t1(&Controleur::ctr_main);
    }

标签: multithreadingvisual-studiowinformsparallel-processingc++-cli

解决方案


推荐阅读