base\timer\timer.h
//-----------------------------------------------------------------------------
// A simple, one-shot timer. See usage notes at the top of the file.
class BASE_EXPORT OneShotTimer : public internal::DelayTimerBase {
public:
OneShotTimer();
explicit OneShotTimer(const TickClock* tick_clock);
OneShotTimer(const OneShotTimer&) = delete;
OneShotTimer& operator=(const OneShotTimer&) = delete;
~OneShotTimer() override;
// Start the timer to run at the given |delay| from now. If the timer is
// already running, it will be replaced to call the given |user_task|.
virtual void Start(const Location& posted_from,
TimeDelta delay,
OnceClosure user_task);
// Start the timer to run at the given |delay| from now. If the timer is
// already running, it will be replaced to call a task formed from
// |receiver->*method|.
template <class Receiver>
void Start(const Location& posted_from,
TimeDelta delay,
Receiver* receiver,
void (Receiver::*method)()) {
Start(posted_from, delay, BindOnce(method, Unretained(receiver)));
}
// Run the scheduled task immediately, and stop the timer. The timer needs to
// be running.
void FireNow();
private:
void OnStop() final;
void RunUserTask() final;
void EnsureNonNullUserTask() final;
OnceClosure user_task_;
};
//-----------------------------------------------------------------------------
// A simple, repeating timer. See usage notes at the top of the file.
class BASE_EXPORT RepeatingTimer : public internal::DelayTimerBase {
public:
RepeatingTimer();
explicit RepeatingTimer(const TickClock* tick_clock);
RepeatingTimer(const RepeatingTimer&) = delete;
RepeatingTimer& operator=(const RepeatingTimer&) = delete;
~RepeatingTimer() override;
RepeatingTimer(const Location& posted_from,
TimeDelta delay,
RepeatingClosure user_task);
RepeatingTimer(const Location& posted_from,
TimeDelta delay,
RepeatingClosure user_task,
const TickClock* tick_clock);
// Start the timer to run at the given |delay| from now. If the timer is
// already running, it will be replaced to call the given |user_task|.
virtual void Start(const Location& posted_from,
TimeDelta delay,
RepeatingClosure user_task);
// Start the timer to run at the given |delay| from now. If the timer is
// already running, it will be replaced to call a task formed from
// |receiver->*method|.
template <class Receiver>
void Start(const Location& posted_from,
TimeDelta delay,
Receiver* receiver,
void (Receiver::*method)()) {
Start(posted_from, delay, BindRepeating(method, Unretained(receiver)));
}
const RepeatingClosure& user_task() const { return user_task_; }
private:
// Mark this final, so that the destructor can call this safely.
void OnStop() final;
void RunUserTask() override;
void EnsureNonNullUserTask() final;
RepeatingClosure user_task_;
};
==========================================
一、单次运行定时器例子:
base::OneShotTimer timer_;
void test01(){
}
timer_.Start(FROM_HERE, base::Seconds(5),
&test01);
==========================================
二、重复运行timer
base::RepeatingTimer timer02_;
void test02(){
}
timer02_.Start(
FROM_HERE, base::Days(1),
base::BindRepeating(&test02));
Chromium base库定时器使用说明c++
最新推荐文章于 2024-10-19 15:29:34 发布