fix: Diffing task not being properly cancelable

Fixes #2383
This commit is contained in:
WerWolv
2025-08-08 17:47:15 +02:00
parent a9faba5cec
commit 725462b222
3 changed files with 13 additions and 6 deletions

View File

@@ -194,6 +194,10 @@ namespace hex {
// Store information about the caught exception
m_exceptionMessage = message;
m_hadException = true;
// Call the interrupt callback on the current thread if one is set
if (m_interruptCallback)
m_interruptCallback();
}

View File

@@ -42,6 +42,7 @@ namespace hex::plugin::diffing {
TaskHolder m_diffTask;
std::atomic<bool> m_analyzed = false;
std::atomic<bool> m_analysisInterrupted = false;
ContentRegistry::Diffing::Algorithm *m_algorithm = nullptr;
};

View File

@@ -19,7 +19,7 @@ namespace hex::plugin::diffing {
this->reset();
});
EventDataChanged::subscribe(this, [this](prv::Provider *) {
m_analyzed = false;
m_analysisInterrupted = m_analyzed = false;
});
// Set the background highlight callbacks for the two hex editor columns
@@ -94,7 +94,9 @@ namespace hex::plugin::diffing {
void ViewDiff::analyze(prv::Provider *providerA, prv::Provider *providerB) {
auto commonSize = std::max(providerA->getActualSize(), providerB->getActualSize());
m_diffTask = TaskManager::createTask("hex.diffing.view.diff.task.diffing", commonSize, [this, providerA, providerB](Task &) {
m_diffTask = TaskManager::createTask("hex.diffing.view.diff.task.diffing", commonSize, [this, providerA, providerB](Task &task) {
task.setInterruptCallback([this]{ m_analysisInterrupted = true; });
auto differences = m_algorithm->analyze(providerA, providerB);
auto providers = ImHexApi::Provider::getProviders();
@@ -189,7 +191,7 @@ namespace hex::plugin::diffing {
}
// Analyze the providers if they are valid and the user selected a new provider
if (!m_analyzed && a.provider != -1 && b.provider != -1 && !m_diffTask.isRunning() && m_algorithm != nullptr) {
if (!m_analyzed && !m_analysisInterrupted && a.provider != -1 && b.provider != -1 && !m_diffTask.isRunning() && m_algorithm != nullptr) {
const auto &providers = ImHexApi::Provider::getProviders();
auto providerA = providers[a.provider];
auto providerB = providers[b.provider];
@@ -229,11 +231,11 @@ namespace hex::plugin::diffing {
ImGui::SameLine();
// Draw first provider selector
if (drawProviderSelector(a)) m_analyzed = false;
if (drawProviderSelector(a)) m_analysisInterrupted = m_analyzed = false;
// Draw second provider selector
ImGui::TableNextColumn();
if (drawProviderSelector(b)) m_analyzed = false;
if (drawProviderSelector(b)) m_analysisInterrupted = m_analyzed = false;
}
ImGui::EndDisabled();
@@ -405,7 +407,7 @@ namespace hex::plugin::diffing {
ImGui::PushID(algorithm.get());
if (ImGui::Selectable(Lang(algorithm->getUnlocalizedName()))) {
m_algorithm = algorithm.get();
m_analyzed = false;
m_analysisInterrupted = m_analyzed = false;
}
ImGui::PopID();
}