c++ - 函数内的异步定时器

标签 c++ c++11 asynchronous timer ncurses

我有一个在 NCurses 界面中从左向右滚动文本的功能,但它依赖于每 500 毫秒左右的计时器来正确地在前面写出一个新字符并删除后面的前一个字符。这是代码:

void GekkoFyre::TuiHangouts::gui_scrollText(WINDOW *display, const char *msg,
                                            const int &msgPosY)
{
    size_t maxX = getmaxx(display);
    size_t msgLen = strlen(msg);
    short origPosY = 0;
    short origPosX = 0;
    getyx(display, origPosY, origPosX); // Obtain the original cursor position

    if (msgLen > maxX) {
        // size_t diff = (msgLen - maxX);
        size_t i = 0;
        for (i = maxX; i < msgLen; ++i) {
            // Scroll the message from left to right, then vice versa, so that it fits within the
            // designated window.
            move(msgPosY, 0);
            clrtoeol(); // Delete just the given line, <http://stackoverflow.com/questions/5072881/how-to-clear-a-specific-line-with-ncurses>
            move(origPosY, origPosX); // Go back to the original cursor position
            std::string tmp((msgLen - i), msg[(msgLen - i)]);
            wattron(display, A_REVERSE); // Highlight selection
            mvwaddstr(display, msgPosY, 0, tmp.c_str());
            std::this_thread::sleep_for(std::chrono::milliseconds(500));
        }
    }
}

老实说,我不知道这是否有效,因为我一直无法让它发挥作用。但是有没有像 std::this_thread::sleep_for(std::chrono::milliseconds(500)); 这样的计时器,您可以在函数中使用但它是异步的?那将是美丽的!我做了一些研究并遇到了 std::async 和 std::thread,但它们依赖于函数的外部。

非常感谢任何帮助,谢谢。

最佳答案

But is there a timer just like, std::this_thread::sleep_for(std::chrono::milliseconds(500));, that you can use within a function but is asynchronous?

不,你不能。您实际上从异步行为 一词中领悟到了什么。您需要在代码中的其他地方有一个连接点来捕获计时器已用事件。

关于c++ - 函数内的异步定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35539786/

相关文章:

c++ - 使用 GDB 调试时段错误消失

c++ - 最小化无边界对话框问题

scala - Play 2.0 Framework (scala) 中的异步结果

c++ - 如何在 c++ 中对 double 或 float 的尾数和指数部分进行(快速)操作?

c++ - 如何将多个 std::function 合并为一个?

c++ - 从(std::function)输出lambda中的引用传递值?

c++ - 为什么我无法使用 C++11 基于范围的 for 循环迭代 vector ?

c++ - 哪种重载组合性能最高?

ios - 从 Swift 函数中的异步调用返回数据

python - 是否可以使用 Python 异步发送许多请求