gpt4 book ai didi

c++ - 在 C++ 程序中有时间等待

转载 作者:行者123 更新时间:2023-11-30 03:57:17 31 4
gpt4 key购买 nike

我读了this书。对于我安装的图形 FLTK在我的编译器 MS visual studio 2012 上。我使用的机器是 MS Windows 7。
我一直读到那本书第 17 章,但我还没有研究过等待的任何方法。等待是指执行一条语句,让系统等待一段时间,然后执行第二条语句。

下面是一个在窗口上绘制两个形状的简单示例。本书使用的图形库是here .

例如,在这段代码中,我有两个具有两个不同位置和不同半径的
我想附加第一个 circle (c1),然后等待一秒钟,分离 c1 并附加 c2这次。请问等待一秒(或更长时间)的最简单方法是什么?

#include <Windows.h>
#include <GUI.h>
using namespace Graph_lib;

//---------------------------------

class Test : public Window {

public:
Test(Point p, int w, int h, const string& title):
Window(p, w, h, title),
quit_button(Point(x_max()-90,20), 60, 20, "Quit", cb_quit),
c1(Point(100,100), 50),
c2(Point(300,200), 100) {
attach(quit_button);
attach(c1);
attach(c2);
}

private:
Circle c1, c2;

Button quit_button;

void quit() { hide(); }
static void cb_quit(Address, Address pw) {reference_to<Test>(pw).quit();}
};

//------------------

int main() {
Test ts(Point(300,250), 800, 600, "Test");
return gui_main();
}

最佳答案

如果您使用的是 C++11:

std::this_thread::sleep_for(std::chrono::seconds(1));

否则使用 Windows 功能 sleep 。

如果你想等待而不阻塞主线程,你可以使用 std::async:

#include <future>

// in your Test's constructor
std::async([&]()
{
attach(quit_button);
attach(c1);
std::this_thread::sleep_for(std::chrono::seconds(1));
attach(c2);
});

虽然这可能行不通,因为我不太了解您正在使用的库。

关于c++ - 在 C++ 程序中有时间等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28088889/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com