如果想在一个C++对象中使用你刚实现的SequencedTaskRunner类,并且把改对象中的一个方法当做task来定时调用,该如何用C++实现呢
时间: 2024-05-24 13:12:59 浏览: 12
假设该C对象为MyObject,其中有一个方法叫做myMethod。以下是实现的步骤:
1. 在MyObject的头文件中引入SequencedTaskRunner的头文件,例如:#include "sequenced_task_runner.h"
2. 在MyObject中创建一个SequencedTaskRunner对象,例如:
SequencedTaskRunner runner;
3. 在MyObject的构造函数中初始化runner,例如:
MyObject() {
runner = SequencedTaskRunner::CreateSequencedTaskRunnerWithTraits({base::MayBlock()});
}
4. 在MyObject的析构函数中销毁runner,例如:
~MyObject() {
runner.Reset();
}
5. 在MyObject中定义一个方法,用于把myMethod作为task提交给runner定时调用,例如:
void ScheduleMethodCall(int64_t delay_ms) {
base::Closure closure = base::Bind(&MyObject::myMethod, base::Unretained(this));
runner->PostDelayedTask(FROM_HERE, closure, base::TimeDelta::FromMilliseconds(delay_ms));
}
6. 在需要定时调用myMethod的地方调用ScheduleMethodCall方法,例如:
MyObject obj;
obj.ScheduleMethodCall(1000); // 延迟1秒钟调用myMethod
这样就可以在C对象中使用SequencedTaskRunner类,把其中的一个方法当做task定时调用了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)