windows 暂停时加一秒的计时器

lskq00tm  于 2023-02-16  发布在  Windows
关注(0)|答案(1)|浏览(128)

这应该是一个正常的计时器,它会一直运行到被暂停或退出,代码由NextStep()函数退出,计时器应该暂停并保持暂停状态,直到它被再次按下。
但是,当我按空格键时,计时器只有在向计时器添加另一个数字后才会停止。
这个问题的一个例子:
01秒
02秒
我按空格键。
03秒(+1秒)
计时器暂停。
另一方面,执行NextStep()的代码工作正常,没有任何延迟。
我试着用不同的方法重写它,但都不起作用。

#pragma once

#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include <stdlib.h>
#include <iomanip>
#include <math.h>
#include <windows.h>
#include <conio.h>
#include <thread>

using namespace std;



void NextStep();
bool pause = false;
bool stop = false; 


void wait(int milliseconds)
{
    int counter = 0;

    while (pause) {
        char ch = _getch();
        if (ch == ' ')
        {
            pause = !pause;
        }
        else if (ch == 27)
        {
            cout << "\033c";
            NextStep();
            stop = true;

        }
    }

    while (counter < milliseconds && !stop)
    {

        if (pause) {
            continue;
        }

        if (pause == false) {
            this_thread::sleep_for(chrono::milliseconds(1));
            counter++;
        }
    }

}

void timer() 
{
    int seconds = 0;
    int minutes = 0;
    int hours = 0;

    
    while (true) 
    {

        cout << "\033c";
        cout << setfill(' ') << setw(55) << "    Timer   \n";
        cout << "                                          ";
        cout << setfill('0') << setw(2) << hours << "h ";
        cout << setfill('0') << setw(2) << minutes << "min ";
        cout << setfill('0') << setw(2) << seconds << "sec ";
        wait(60);

        seconds++;

        if (seconds == 60) {
            minutes++;
            seconds = 0;
        }
        else if (minutes == 60)
        {
            hours++;
            minutes = 0;
            seconds = 0; 
        }
        if (_kbhit())
        {
            char ch = _getch();
            if (ch == ' ')
            {
                pause = !pause;
            }
            else if (ch == 27)
            {
                cout << "\033c";
                NextStep();
                stop = true;
                break;
            }
        }
    }

}

下面是NextStep()函数:

void NextStep()
{
    string option;
    correct = false; 
    cout << "\033c";
    cout << "Loading...";
    Sleep(1300);
    cout << "\033c";
    cout << "What would you like to do next?" << endl;
    cout << "" << endl;
    cout << "To change your password TYPE: password" << endl;
    cout << "To use a calculator TYPE: calculator" << endl; 
    cout << "To use a timer TYPE: timer" << endl; 
    cout << "" << endl;
    cout << "Type your choice: ";
    UserInput();

}
9rnv2umw

9rnv2umw1#

我不确定我是否理解了你代码的所有工作原理,但是我想我理解了你需要一个可以启动、暂停和重新启动的计时器。另外,如果我理解正确的话,计时器不应该受到线程处于睡眠状态这一事实的影响。
因此,下面是这样一个计时器的实现,它可能会对您有所帮助(至少对您的问题的一部分):

#include <chrono>

template<typename T>
class Timer
{

public:

    void start()
    {
        m_start = std::chrono::steady_clock::now();
    }

    void pause()
    {
        m_elapsed += get_current_elapsed();
        m_start = {};
    }

    void reset()
    {
        m_start = {};
        m_elapsed = {};
    }

    int64_t get_elapsed()
    {
        return (m_elapsed + get_current_elapsed()).count();
    }

private:

    std::chrono::time_point<std::chrono::steady_clock> m_start{};
    T m_elapsed{};

    bool is_started()
    {
        return m_start.time_since_epoch() != T{};
    }

    T get_current_elapsed()
    {
        return is_started() ? std::chrono::duration_cast<T>(std::chrono::steady_clock::now() - m_start) : T{};
    }
};

下面是一个如何使用它的示例:

#include <iostream>
#include <thread>

int main()
{
    Timer<std::chrono::seconds> timer;

    timer.start();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    timer.pause(); // "optional"
    std::cout << timer.get_elapsed() << std::endl; // 1

    timer.start(); // "optional"
    std::this_thread::sleep_for(std::chrono::seconds(1));
    timer.pause(); // "optional"
    std::cout << timer.get_elapsed() << std::endl; // 2

    timer.reset();
    timer.start();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    timer.pause(); // "optional"
    std::cout << timer.get_elapsed() << std::endl; // 1

    return 0;
}

Timer是一个模板,因此您可以使用以下任意类型来代替std::chrono::seconds,具体取决于计时器所需的精度:

  • 标准::计时::纳秒
  • 标准::计时::微秒
  • 标准::计时::毫秒
  • 标准::计时::秒
  • 标准::计时::分钟
  • 标准::计时::小时
  • 标准::时间::天
  • 标准::年代::年

相关问题