Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;刷新线程_C++_Multithreading - Fatal编程技术网

C++ C++;刷新线程

C++ C++;刷新线程,c++,multithreading,C++,Multithreading,我有一个我正在做的项目,我有一个显示机器人坐标的标签。然而,要做到这一点(由于具体情况),我需要让函数每1秒运行一次,以给出这些值。 每秒钟都是这样的: label1->Text = read_position(axis1); 但是我不知道怎么做。。能请人帮忙吗?谢谢 编辑:使用Visual Studio 2015您添加了“多线程”标记,因此我认为您可以自由使用多线程。因此,实现这一点的方法是启动一个新线程,该线程将执行以下操作: while( ! instructed_to_quit(

我有一个我正在做的项目,我有一个显示机器人坐标的标签。然而,要做到这一点(由于具体情况),我需要让函数每1秒运行一次,以给出这些值。 每秒钟都是这样的:

label1->Text = read_position(axis1);
但是我不知道怎么做。。能请人帮忙吗?谢谢
编辑:使用Visual Studio 2015

您添加了“多线程”标记,因此我认为您可以自由使用多线程。因此,实现这一点的方法是启动一个新线程,该线程将执行以下操作:

while( ! instructed_to_quit() )
{
    give_him_those_values();
    sleep_for_a_second();
}

具体如何实现这些步骤在很大程度上取决于您运行的是什么类型的系统,您对此一直很保密,因此如果您告诉我们更多信息,我们可能会提供更多帮助。

如果您使用一些GUI框架,我建议您不要为这么简单的事情使用多线程。例如,在win32中,您可以使用SetTimer函数。

因为您使用的是Visual Studio 2015,所以您可以使用C++11的标准线程和原子变量。有一些不同的可能解决方案,以下是其中之一

static MyRobotForm myRobot(void);
static std::thread reader;
static std::atomic<double*> coordinates(nullptr);
static std::atomic<bool> shutdown(false);

static void position_reader() {
   // loop until app is alive
   while(!shutdown) {
       // fetch the coordinates array
       double *temp = read_all_axis(myRobot.Cp, decision);
       // atomically replace the old unused value or nullptr
       temp = coordinates.exchange(temp);
       // it is safe to delete nullptr
       delete temp;
       // sleep for the proper time
       std::this_thread::sleep_for(1s);
   }
   // finicky but correct
   delete coordinates.load();
}

int main(int argc, char **argv) {
    // start the reading thread
    reader = std::thread(position_reader);
    // where depends on gui toolkit ???
    // atomically seize the current value
    double *temp = coordinates.exchange(nullptr);
    if(temp != nullptr) {
        label1->Text = std::string(/*decide how to display temp*/);
        delete temp;
    }
    // on application exit
    shutdown = true;
    reader.join();
    return 0;
}
静态MyRobotForm myRobot(无效);
静态std::线程读取器;
静态标准::原子坐标(nullptr);
静态标准::原子关机(假);
静态空隙位置_读取器(){
//循环,直到应用程序处于活动状态
而(!关机){
//获取坐标数组
double*temp=读取所有轴(myRobot.Cp,decision);
//以原子方式替换旧的未使用值或nullptr
温度=坐标交换(温度);
//删除nullptr是安全的
删除临时文件;
//适时睡觉
std::this_线程::sleep_for(1s);
}
//挑剔但正确
删除坐标;
}
int main(int argc,字符**argv){
//开始阅读
读卡器=标准::螺纹(位置\读卡器);
//哪里依赖gui工具包???
//原子地获取当前值
double*temp=坐标交换(nullptr);
如果(温度!=nullptr){
label1->Text=std::string(/*决定如何显示temp*/);
删除临时文件;
}
//应用程序退出时
关机=真;
reader.join();
返回0;
}

我还没有测试过它,但它应该可以工作。您正在使用什么GUI工具包?*)

您可能不想让另一个线程给GUI线程一个值。当GUI部分通过重画并更改标签时,会发生不好的事情。对于GUI线程来说,获得所需的值可能更安全。循环直到GUI关闭,所以我不认为这会麻烦(?)你能给我类似的伪代码吗?我对穿线毫无经验,对不起!我使用VisualStudio 2015,C++语言:)取决于运行的是什么样的系统,可能是在允许应用程序终止之前,每个线程都必须终止。因此,如果不希望应用程序在终止时出现挂起状态,您可能必须显式地告诉每个线程退出。由于gui的所有选项卡都取决于位置,而且我总是需要这些信息,所以我只希望在.exe关闭时停止“刷新”。我上面给出的是伪代码。如果您使用的是Microsoft Visual Studio 2015,那么您的系统很可能是Windows。如果您正在编写一个普通的Win32可执行文件,那么函数是
::Sleep(int毫秒)
,通常在
windows.h
中找到。(但您可能需要查看
Synchapi.h
)如果您是为.Net编写代码,那么函数将是
System.Threading.Thread.Sleep(int毫秒)
。有没有关于从哪里了解更多信息的建议?在MFC和ATL/WTL中,窗口类中将有更方便的包装方法。此外,您应该在窗口中实现OnTimer处理程序,该窗口将从timer.Hi接收定期消息。首先,我不认为我能回答关于GUI工具包的问题,我是新手,我不知道(这可能有用吗?)。因为这是用于GUI的,我没有main,我应该把代码放在哪里?实际上还有5个坐标我需要访问,不仅仅是一个。记住我的第一个问题,我的GUI中不能有main,我有一个函数read_all_axis,返回一个包含所有位置的数组,如果有帮助的话!究竟是什么?无论如何,您是如何构建GUI的?返回一个双精度数组(double*read\u all\u axis(TCommPort*Cp,int decision))。由于VS2015不允许创建C++ GUI(我想)在VS2013中创建的朋友,所以我不知道。由于TCommPort不是静态的,我必须使位置_reader only无效,这有问题吗?这也会发生:谢谢你的耐心!:)