1

I'm trying to create a simple non-blocking timer in C++ that can be reset. I've google searched for a while to find some code examples but everything I find is either too complicated to understand or does not compile without extra non example code.

I have a program that logs a key press to a file, but I want this file to be written over one seconds after the key has been pressed unless another key has been pressed before the second is up.

I believe I should use Win32 timers, (I am using windows) however I can't find a simple compilable example of their use.

for example this method taken from the info page:

SetTimer(hwnd,                // handle to main window 
IDT_TIMER1,                   // timer identifier 
1000,                        // 1-second interval 
(TIMERPROC) MyTimerProc);     // no timer callback 


VOID CALLBACK MyTimerProc( 
HWND hwnd,        // handle to window for timer messages 
UINT message,     // WM_TIMER message 
UINT idTimer,     // timer identifier 
DWORD dwTime)     // current system time 
{

// my proceedure code 

FILE *OUTPUT_FILE;
OUTPUT_FILE = fopen("LOG.TXT", "w");
fprintf(OUTPUT_FILE, "%s", "");
fclose(OUTPUT_FILE);

// writes a blank text file.


KillTimer(hwnd, IDT_TIMER1); // stop timer after one call
}

doesn't compile because the IDT_TIMER1 hasnt' been defined I think.

Also, I'm not sure if this would be reset by calling it twice or would just start two separate calls 1 second apart.

Any help greatly appreciated.

4

4 回答 4

0

您有责任提供ID_TIMER1价值。我相信你(99%)还必须有一个 Windows 消息循环才能工作。

这里有几个最小的、可编译的例子

于 2012-08-04T00:01:59.460 回答
0

只需使用硬件性能计时器,可以通过 google 快速找到,或者如果您想要更多跨平台的东西使用 timegettime 方法,也可以通过 google 找到,不需要 Win32 消息循环。

于 2012-08-04T00:43:17.647 回答
0

以下控制台程序有效。SetTimer 它使用消息循环中的 then 循环设置计时器。消息循环接收和处理WM_TIMER消息,并且每个时间间隔都会调用计时器回调。它不会阻塞。

#define STRICT 1 
#include <windows.h>
#include <iostream.h>

VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime) 
{
  cout << "Time: " << dwTime << '\n';
  cout.flush();
}

int main(int argc, char *argv[], char *envp[]) 
{
    int Counter=0;
    MSG Msg;

    UINT TimerId = SetTimer(NULL, 0, 500, &TimerProc); //SetTimer

    cout << "TimerId: " << TimerId << '\n';
   if (!TimerId)
    return 16;

   while (GetMessage(&Msg, NULL, 0, 0)) 
   {
        ++Counter;
        if (Msg.message == WM_TIMER)
        cout << "Counter: " << Counter << "; timer message\n";
        else
        cout << "Counter: " << Counter << "; message: " << Msg.message << '\n';
        DispatchMessage(&Msg);
    }

   KillTimer(NULL, TimerId);
return 0;
}
于 2012-08-04T14:26:27.553 回答
0

您不需要窗口甚至事件循环。查看WaitForSingleObject 函数

这是一些代码

HANDLE hStdin; 
hStdin = GetStdHandle(STD_INPUT_HANDLE); 

FILE *OUTPUT_FILE;
OUTPUT_FILE = fopen("LOG.TXT", "w");

bool previous_input_written = TRUE;

switch (WaitForSingleObject(hStdin, 1000) {

  case WAIT_OBJECT_0:  
    // input happened on hStdin 
    if (!previous_input_written) {
      // the previous input was not saved to file
      fprintf(OUTPUT_FILE, ...
      previous_input_written= TRUE;
    }
    ReadConsoleInput(hStdin, ...
    previous_input_written = FALSE;

  case WAIT_TIMEOUT: 
    // the 1000ms delay timed out, write to file
    fprintf(OUTPUT_FILE, ...
    previous_input_written= TRUE;
}


fclose(OUTPUT_FILE);
于 2012-08-04T10:10:37.150 回答