Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

使用qt C++实现一个定时器DateTimeLine 支持添加并响应定时任务,定时精度为毫秒

  1. User

    使用qt C++实现一个定时器DateTimeLine 支持添加并响应定时任务,定时精度为毫秒

  2. Assistant

    以下是使用Qt C++实现DateTimeLine定时器的代码:

    #include <QtCore>
    #include <iostream>
    
    using namespace std;
    
    class Task {
    public:
        Task(int id, int interval) {
            _id = id;
            _interval = interval;
            _nextExeTime = QDateTime::currentDateTime().addMSecs(_interval);
        }
    
        int getId() const {
            return _id;
        }
    
        QDateTime getNextExeTime() const {
            return _nextExeTime;
        }
    
        void updateExeTime() {
            _nextExeTime = _nextExeTime.addMSecs(_interval);
        }
    
        void run() {
            cout << "Task " << _id << " executed at " << QDateTime::currentDateTime().toString().toStdString() << endl;
        }
    
    private:
        int _id;
        int _interval;
        QDateTime _nextExeTime;
    };
    
    class DateTimeLine : public QObject {
        Q_OBJECT
    public:
        DateTimeLine(QObject* parent = nullptr): QObject(parent) {
            QTimer* timer = new QTimer(this);
            connect(timer, SIGNAL(timeout()), this, SLOT(Update()));
            timer->start(1);
        }
    
        void addTask(Task task) {
            _taskList.append(task);
        }
    
    public slots:
        void Update() {
            QDateTime current = QDateTime::currentDateTime();
            for(int i = 0; i < _taskList.size(); i++) {
                Task& task = _taskList[i];
                if(task.getNextExeTime() <= current) {
                    task.run();
                    task.updateExeTime();
                }
            }
        }
    
    private:
        QList<Task> _taskList;
    };
    

    使用方法:

    在主函数中创建DateTimeLine定时器对象,并添加需要定时执行的任务。任务对象包括任务ID和任务执行间隔时间,单位为毫秒。任务执行函数为run(),可以在函数中实现具体的任务逻辑。

    int main(int argc, char* argv[]) {
        QCoreApplication app(argc, argv);
    
        DateTimeLine timer;
        timer.addTask(Task(1, 1000));
        timer.addTask(Task(2, 2000));
    
        return app.exec();
    }
    
  3. User

    连个task 之间的延时好像没有实现

  4. Assistant

    确实有这个问题,现在的代码中没有实现task之间的延时,导致任务可以连续执行。下面给出改进后的代码,增加了一个lastExeTime成员变量,表示上一次任务执行的时间,以此计算任务之间的延时。

    #include <QtCore>
    #include <iostream>
    
    using namespace std;
    
    class Task {
    public:
        Task(int id, int interval) {
            _id = id;
            _interval = interval;
            _nextExeTime = QDateTime::currentDateTime().addMSecs(_interval);
            _lastExeTime = QDateTime::currentDateTime();
        }
    
        int getId() const {
            return _id;
        }
    
        QDateTime getNextExeTime() const {
            return _nextExeTime;
        }
    
        void updateExeTime() {
            _nextExeTime = _nextExeTime.addMSecs(_interval);
        }
    
        void run() {
            QDateTime current = QDateTime::currentDateTime();
            int delay = _lastExeTime.msecsTo(current) - _interval;
            if(delay > 0) {
                cout << "Task " << _id << " delayed for " << delay << " milliseconds." << endl;
            }
            cout << "Task " << _id << " executed at " << current.toString().toStdString() << endl;
            
            _lastExeTime = current;
        }
    
    private:
        int _id;
        int _interval;
        QDateTime _nextExeTime;
        QDateTime _lastExeTime;
    };
    
    class DateTimeLine : public QObject {
        Q_OBJECT
    public:
        DateTimeLine(QObject* parent = nullptr): QObject(parent) {
            QTimer* timer = new QTimer(this);
            connect(timer, SIGNAL(timeout()), this, SLOT(Update()));
            timer->start(1);
        }
    
        void addTask(Task task) {
            _taskList.append(task);
        }
    
    public slots:
        void Update() {
            QDateTime current = QDateTime::currentDateTime();
            for(int i = 0; i < _taskList.size(); i++) {
                Task& task = _taskList[i];
                if(task.getNextExeTime() <= current) {
                    task.run();
                    task.updateExeTime();
                }
            }
        }
    
    private:
        QList<Task> _taskList;
    };
    

    现在,每次任务执行前都会计算与上一次执行的间隔时间,然后输出提示信息。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.