重构后,异步调用方法不再有效

Calling a method async does not work anymore after refactoring

本文关键字:方法 不再 有效 调用 异步 重构      更新时间:2023-10-16

为了将我的GUI与逻辑(从REST服务获取数据(分开,我将一些逻辑重构到控制器中。

现在,似乎只有部分逻辑有效。

重构后的GUI组件看起来像这样(我使用的是JUCE框架(

#pragma once
#include "../../JuceLibraryCode/JuceHeader.h"
#include "../../GUI.Controller/includes/ProjectEntryListController.h"
#include "ProjectEntryListComponent.h"
#include "LocalProjectEntryComponent.h"
class ProjectBrowserTabComponent : public TabbedComponent
{
public:
    ProjectBrowserTabComponent();
    ~ProjectBrowserTabComponent();
private:
    ProjectEntryListComponent m_remote_proj;
    ProjectEntryListComponent m_local_proj;
    ProjectEntryListController *pelccont = new ProjectEntryListController(&m_remote_proj);
    ProjectEntryListController *pelccont2 = new ProjectEntryListController(&m_local_proj);
};

GUI 控制器如下所示:

#define BOOST_THREAD_PROVIDES_FUTURE
#include "../includes/ProjectEntryListController.h"
template<typename R>
bool isReady(std::future<R> const& f)
{
    Logger::writeToLog("check future");
    return f.wait_for(std::chrono::seconds(-1)) == std::future_status::ready;
}
ProjectEntryListController::ProjectEntryListController(ProjectEntryListComponent *comp) {
    m_comp = comp;
    requestProjects();
}
void ProjectEntryListController::requestProjects()
{
    Logger::writeToLog("requesting projects");
    projectsFuture = std::async(std::launch::async, &ProjectsController::getProjects, &pc);
    Logger::writeToLog("requested projects");
}
void ProjectEntryListController::backgroundCheckFuture()
{
    timer = new boost::asio::deadline_timer(io_service, boost::posix_time::seconds(interval_secs));
    timer->async_wait(boost::bind(&ProjectEntryListController::fetchData, this, boost::asio::placeholders::error, timer));
    ioSvcFuture = std::async(std::launch::async, static_cast<size_t(boost::asio::io_service::*)()>(&boost::asio::io_service::run), &io_service);
}
void ProjectEntryListController::initData() {
    requestProjects();
    backgroundCheckFuture();
}
void ProjectEntryListController::fetchData(const boost::system::error_code& /*e*/,
    boost::asio::deadline_timer* tmr) {
    if (isReady(projectsFuture)) {
        projects = projectsFuture.get();
        for (auto project : projects)
        {
            ProjectEntryComponent *pec = new ProjectEntryComponent(std::to_string(project.getId()), "222");
            m_comp->addListEntry(pec);
            m_comp->repaint();
        }
        Logger::writeToLog("got projs");
    }
    else {
        tmr->expires_at(tmr->expires_at() + boost::posix_time::seconds(interval_secs));
        tmr->async_wait(boost::bind(&ProjectEntryListController::fetchData, this, boost::asio::placeholders::error, tmr));
    }
}

requestProjects方法的日志消息显示在我的控制台中,但不会出现我异步调用的getProjects方法的日志消息:

std::vector<Project> ProjectsController::getProjects() {
    std::vector<Project> result;
    if(serviceClient != nullptr) {
        try
        {
            std::this_thread::sleep_for(std::chrono::seconds());
            std::cout << "controller requested projsn";
            result = serviceClient->getAvailableProjects();
        }
        catch (const std::exception&)
        {
        }
    }
    return result;
}

但是,当我调试代码时,调试器(使用 VS 2015(也可以单步执行日志消息。

我做错了什么?

其实我现在解决了这个问题。

1.(我调用了错误的方法requestProjects而不是initData

2.( 我看不到结果,因为缺少ProjectEntryComponent::ProjectEntryComponent(std::string name, std::string version)的实现