具有更高优先级的WM_TIMER

WM_TIMER with higher priority?

本文关键字:WM TIMER 高优先级      更新时间:2023-10-16

我希望我的小游戏节目 FPS 出现在标题中,但它不应该重新计算每一帧和单帧的 FPS。我只想每秒刷新一次 FPS 计数器,所以我尝试使用 SetTimer .问题是计时器只有在我不移动鼠标或按住键的情况下才能工作。据我所知WM_TIMER是低优先级的消息,因此最后处理。有没有办法在任何其他用户输入消息之前处理WM_TIMER消息,或者至少另一种创建第二个滴答计时器的方法?

我也尝试使用TimerProc而不是等待WM_TIMER,但这也没有用。

如何使用单独的后台线程测量它的简短示例。

int iCount;
int iFramesPerSec;
std::mutex mtx;
// this function runs in a separate thread
void frameCount()
{
    while(true){
        std::this_thread::sleep_for(std::chrono::seconds(1));
        std::lock_guard<std::mutex> lg{mtx}; // synchronize access
        iFramesPerSec = iCount; // frames per second during last second that passed
        iCount = 0;
    }
}
// inside window procedure
case WM_PAINT:
    hdc = BeginPaint(hwnd, &ps);
    ...
    std::lock_guard<std::mutex> lg{mtx}; // synchronize access
    ++iCount;
    EndPaint(hwnd, &ps);
    return 0;