【问题标题】:c++ - delayed execution of codec++ - 延迟执行代码
【发布时间】:2016-12-11 18:58:08
【问题描述】:

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

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


}

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

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

【问题讨论】:

  • 1.不要阻止执行。 2. 鼠标点击后启动/重置计数器。 3. 按下按钮时,检查计数器是否低于 5 秒并做出相应反应。 4. 5 秒后停止计数器并设置为零。这种方式不是更合适吗?

标签: c++ timeout


【解决方案1】:

这是我的课程(它并不完美,但你可以看看它是如何完成的)来控制套接字后面的程序的心跳。当 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;
    };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 2014-08-31
    • 2011-10-18
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多