在 Windows 中创建 C++ 非阻塞定时器 [英] Creating a C++ Non-Blocking Timer in Windows

查看:42
本文介绍了在 Windows 中创建 C++ 非阻塞定时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Linux 上,我将使用 fork() 创建一个子进程,它将作为我的倒计时计时器,一旦计时器结束,子进程将向父进程发送一个信号,告诉它计时器已结束.然后父进程应该相应地处理信号.

On Linux, I would create a child process using fork() that will be my count-down timer, and once the timer ends, the child process will send a signal to the parent process to tell it that the timer has ended. The parent process then should handle the signal accordingly.

我不知道如何在 Windows 上执行此操作.这里有些人推荐使用线程,但他们从未写过任何示例代码来展示如何做到这一点.

I have no idea how to do this on windows. Some people here recommended using threads but they never wrote any example code showing how to do that.

最重要的是计时器是非阻塞的,这意味着它在后台继续倒计时,而程序正在接受用户的输入并正常处理.

The most important thing is that the timer is non-blocking, meaning that it remains counting down in the background, while the program is accepting input from the user and handling it normally.

你能告诉我怎么做吗?

该应用程序是一个控制台应用程序.并请给我看示例代码.谢谢!

The application is a console one. And please show me example code. Thanks!

更新:

所以在我阅读了这里的一些建议之后,我在这里搜索了一些答案并找到了这个一个 这很有帮助.

So after I read some of the suggestions here, I searched for some answers here and found this one which was helpful.

然后我写了下面的代码,它可以工作,但不是应该的:

I then wrote the below code, which works, but not as it's supposed to be:

#include <Windows.h>
#include <iostream>
#include <string>
using namespace std;

#define TIMER_VALUE (5 * 1000) //5 seconds = 5000 milli seconds
HANDLE g_hExitEvent = NULL;

bool doneInTime = false;
string name;

bool inputWords();


//The below function will be called when the timer ends
void CALLBACK doWhenTimerEnds(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
{
    if(!doneInTime)
    {
        cout << "\nOut of time ... try again ..." << endl;
        name = "";
        doneInTime = inputWords();
    }
    SetEvent(g_hExitEvent);
}


bool inputWords()
{

    /* doWhenTimerEnds() will be called after time set by 5-th parameter and repeat every 6-th parameter. After time elapses,
    callback is called, executes some processing and sets event to allow exit */
    HANDLE hNewTimer = NULL; 
    BOOL IsCreated = CreateTimerQueueTimer(&hNewTimer, NULL, doWhenTimerEnds, NULL, TIMER_VALUE, 0, WT_EXECUTELONGFUNCTION);

    g_hExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

    cout << "Input your name in 5 seconds .. " << endl;
    std::getline(cin, name);

    DeleteTimerQueueTimer(NULL, hNewTimer, NULL);
    return true;
}

int main()
{
    doneInTime = inputWords();
    cout << "Hello, " << name << "! You're done in time" << endl;

    //WaitForSingleObject(g_hExitEvent, 15000);

    system("pause");
    return 0;
}

问题是,中断的 getline() 永远不会停止,后续的 getline() 甚至会读取之前输入的文本!我该如何解决?如果有更好的方法,请您指出来好吗?

The problem is, the interrupted getline() never stops, and the subsequent getline()'s read even the text previously entered! how can I fix that please? And if there's a better way of doing it, could you please point me out to it?

推荐答案

以下是使用 Windows API 的示例:

Here's an example that works with the Windows API:

#include <windows.h>
#include <iostream>

DWORD WINAPI threadProc()
{
    for (int i = 0; ; ++i)
    {
        std::cout << i << '\n';
        Sleep (1000);
    }

    return 0;
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                    LPSTR lpszCmdLine, int iCmdShow)
{
    CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)threadProc, NULL, 0, NULL);

    int i;
    std::cin >> i;

    return ERROR_SUCCESS;
}

基本上,main 函数创建了一个使用过程 threadProc 执行的线程.您可以将 threadProc 视为线程.一旦结束,线程就结束了.

Basically the main function creates a thread that executes using the procedure threadProc. You can think of threadProc as the thread. Once it ends, the thread ends.

threadProc 大约每秒输出一个运行计数,而主函数等待阻塞输入.一旦给出输入,整个事情就结束了.

threadProc just outputs a running count every second or so, while the main function waits for a blocking input. Once an input is given, the whole thing ends.

另请注意,CreateThread 使用了最少的参数.它返回一个线程句柄,您可以在 WaitForSingleObject 等函数中使用该句柄,最后一个参数可以接收线程 ID.

Also be aware that CreateThread was used with minimal arguments. It returns a handle to the thread that you can use in functions like WaitForSingleObject, and the last argument can receive the thread id.

这篇关于在 Windows 中创建 C++ 非阻塞定时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆