Теперь мой код выглядит так:
Код: Выделить всё
//main.cpp MainFrame constructor, svc_ is the BackendService object
svc_.setProgressCallback([this](const Progress & p) {
wxApp::GetInstance() -> CallAfter([this, p]() {
this -> gaugeActive -> SetValue(p.percent);
//if (p.percent == 100) {
if (p.status == "Done") {
this -> OnStopSvc();
}
});
});
Код: Выделить всё
//main.cpp
void MainFrame::OnButtonStartActive(wxCommandEvent& event) {
if (!this->processing)
{
this->processing = true;
svc_.startLongTask();
}
}
void MainFrame::OnStopSvc()
{
this->processing = false;
svc_.stop();
}
Код: Выделить всё
//Backend.cpp
#include "backend.h"
#include
BackendService::BackendService() : running_(false) {}
BackendService::~BackendService() { stop(); }
void BackendService::setProgressCallback(ProgressCb cb) {
progressCb_ = std::move(cb);
}
void BackendService::startLongTask() {
if (running_) return;
running_ = true;
workerThread_ = std::thread(&BackendService::workerLoop, this);
}
void BackendService::stop() {
if (!running_) return;
running_ = false;
if (workerThread_.joinable()) workerThread_.join();
}
void BackendService::workerLoop() {
for (int i = 0; i
Подробнее здесь: [url]https://stackoverflow.com/questions/79846494/crash-when-using-callbacks-to-separate-backend-from-the-gui[/url]
Мобильная версия