From ce2c269ad9179516335594f8e890131c7e296546 Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Fri, 10 Nov 2023 17:27:58 +0800 Subject: [PATCH] stop from UI in a new thread --- twain/ds/scanner.cpp | 30 +++++++++++++++++++----------- twain/ds/scanner.h | 3 ++- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/twain/ds/scanner.cpp b/twain/ds/scanner.cpp index 89a11b8..97b226a 100644 --- a/twain/ds/scanner.cpp +++ b/twain/ds/scanner.cpp @@ -264,6 +264,9 @@ scanner::~scanner() if (thread_starting_.get() && thread_starting_->joinable()) thread_starting_->join(); thread_starting_.reset(); + if (thread_stop_.get() && thread_stop_->joinable()) + thread_stop_->join(); + thread_stop_.reset(); } void scanner::get_scanner_name(const char* model, std::vector& names) @@ -398,7 +401,11 @@ void scanner::on_ui_event(int uev, void* sender) { // scan cancelled - stop(); // nothing to do, the finishing work do in SANE_EVENT_SCAN_FINISHED + if (thread_stop_.get() && thread_stop_->joinable()) + thread_stop_->join(); + thread_stop_.reset(new std::thread(&scanner::stop, this)); + return; // notify in SCAN_FINISHED + //stop(); // nothing to do, the finishing work do in SANE_EVENT_SCAN_FINISHED } else if (uev == SANE_EVENT_SCAN_FINISHED) { @@ -824,19 +831,20 @@ int scanner::thread_start(void) ui_notify(SANE_EVENT_SCAN_FINISHED, (void*)sane_helper_->invoke_sane_strstatus((SANE_Status)ret), ret); // block ? - while (!is_scanning_) - { -#ifdef WIN32 - MSG msg = { 0 }; - PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE); -#else - std::this_thread::sleep_for(std::chrono::milliseconds(3)); -#endif - } +// while (!is_scanning_) +// { +//#ifdef WIN32 +// MSG msg = { 0 }; +// PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE); +//#else +// std::this_thread::sleep_for(std::chrono::milliseconds(3)); +//#endif +// } } } - else + else #endif + if (!scan_working_) { if (callback::close_ui) callback::close_ui(UI_INDICATOR); diff --git a/twain/ds/scanner.h b/twain/ds/scanner.h index 8cc2e71..ebd55e2 100644 --- a/twain/ds/scanner.h +++ b/twain/ds/scanner.h @@ -78,7 +78,8 @@ class scanner : public ISaneInvoker, virtual public refer int thread_start(void); std::unique_ptr thread_starting_; - bool scan_working_ = false; + std::unique_ptr thread_stop_; // stop阻塞到工作线程结束,在结束回调后才返回。为不阻塞界面,在线程中停止 + bool scan_working_ = false; // start阻塞到有一张图片才返回,如果第一张出现错误(比如卡纸),则回调先于start返回结束标志。该变量用于协调FINISH事件通知 template