c++ - 延迟执行代码

标签 c++ timeout

假设我们在一个函数中,只要按下鼠标按钮就会被调用

static inline LRESULT CALLBACK WndProc(const int code, const WPARAM wParam, const LPARAM lParam){


}

我现在想在 5 秒内没有按下任何按钮后执行一些代码。如果 2 秒后,用户单击鼠标按钮,“计时器”应重置并再等待 5 秒。

这甚至可以用 C++ 完成吗?如果我使用 Sleep(5000),如果中间按下另一个按钮,我无法阻止代码运行。

最佳答案

这是我的类(它并不完美,但你可以看看它是如何完成的)来控制套接字后面的程序的心跳。当调用 beat() 方法时,计时器被“重置”。

    class HeartbeatController
    {
    private:
        using ms = std::chrono::milliseconds;
    public:
        HeartbeatController(std::function<void()> &heartbeatLostCallback, 
                            const ms &panicTime = ms{5000}, //time in milliseconds, after which panic code will be executed
                            const ms &checkDuration = ms{ 1000 }) noexcept :
            heartbeatLostCallback{ heartbeatLostCallback }
        {}

        ~HeartbeatController() = default;

        HeartbeatController(HeartbeatController &&other) :
            heartbeatLostCallback{ std::move(other.heartbeatLostCallback) },
            loopThread{ std::move(other.loopThread) },
            lastBeat{ std::move(other.lastBeat) },
            panicTime{ std::move(other.panicTime) },
            checkDuration{ std::move(other.checkDuration) }
        {}

        HeartbeatController& operator=(HeartbeatController &&other)
        {
            heartbeatLostCallback = std::move(other.heartbeatLostCallback);
            loopThread = std::move(other.loopThread);
            lastBeat = std::move(other.lastBeat);
            panicTime = std::move(other.panicTime);
            checkDuration = std::move(other.checkDuration);

            return *this;
        }

        HeartbeatController(const HeartbeatController&) = delete;
        HeartbeatController& operator=(const HeartbeatController&) = delete;

        void interrupt() noexcept
        {
            interrupted = true;
        }

        void beat() noexcept
        {
            lastBeat = Clock::now();
        }

        void start()
        {
            auto loop = [this]
            {
                while (!interrupted)
                {
                    if (Clock::now() - lastBeat > panicTime)
                        heartbeatLostCallback(); //here you can insert some your code which you wanna execute after no beat() for panicTime duration

                    std::this_thread::sleep_for(checkDuration);
                }
            };

            lastBeat = Clock::now();

            loopThread = std::thread{ loop };
        }

    private:
        using Clock = std::chrono::system_clock;

        std::reference_wrapper<std::function<void()>> heartbeatLostCallback;
        std::thread loopThread;

        std::chrono::time_point<Clock> lastBeat;
        std::chrono::milliseconds panicTime;
        std::chrono::milliseconds checkDuration;

        bool interrupted = false;
    };

关于c++ - 延迟执行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38787898/

相关文章:

python - 如何使用带超时的 concurrent.futures?

python - 使用 paramiko 防止 SFTP/SSH session 超时

http - RestSharp RestClient 的默认超时值是多少?

PHPExcel 发生超时

c++ - 如何将数组的偶数和奇数相加

C++ 检查队列是否已满?

c++ - 枚举作为具有返回值的类成员函数

timeout - 超时后是否需要调用 EndInvoke?

c++ - Boost named_mutex 无法在不同用户创建的进程之间共享

python - 为什么C++和Python按位移位运算符的结果不同?