asynchelper.cpp 823 B

123456789101112131415161718192021222324252627282930313233
  1. #include "asynchelper.h"
  2. #include <QDebug>
  3. AsyncHelper *AsyncHelper::m_instance = nullptr;
  4. AsyncHelper *AsyncHelper::instance()
  5. {
  6. if (!m_instance) {
  7. m_instance = new AsyncHelper();
  8. }
  9. return m_instance;
  10. }
  11. AsyncHelper::AsyncHelper(QObject *parent)
  12. : QObject(parent)
  13. {}
  14. // 修改安全执行回调的辅助函数实现
  15. void safeExecuteCallback(const std::function<void()> &callback, const QString &callbackType)
  16. {
  17. if (!callback) {
  18. qDebug() << "回调函数为空,跳过执行" << callbackType;
  19. return;
  20. }
  21. try {
  22. callback();
  23. } catch (const std::exception &e) {
  24. qWarning() << "执行" << callbackType << "回调时发生异常:" << e.what();
  25. } catch (...) {
  26. qWarning() << "执行" << callbackType << "回调时发生未知异常";
  27. }
  28. }