stop from UI in a new thread

This commit is contained in:
gb 2023-11-10 17:27:58 +08:00
parent 525dfeebac
commit ce2c269ad9
2 changed files with 21 additions and 12 deletions

View File

@ -264,6 +264,9 @@ scanner::~scanner()
if (thread_starting_.get() && thread_starting_->joinable()) if (thread_starting_.get() && thread_starting_->joinable())
thread_starting_->join(); thread_starting_->join();
thread_starting_.reset(); 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<std::string>& names) void scanner::get_scanner_name(const char* model, std::vector<std::string>& names)
@ -398,7 +401,11 @@ void scanner::on_ui_event(int uev, void* sender)
{ {
// scan cancelled // 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) 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); ui_notify(SANE_EVENT_SCAN_FINISHED, (void*)sane_helper_->invoke_sane_strstatus((SANE_Status)ret), ret);
// block ? // block ?
while (!is_scanning_) // while (!is_scanning_)
{ // {
#ifdef WIN32 //#ifdef WIN32
MSG msg = { 0 }; // MSG msg = { 0 };
PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE); // PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE);
#else //#else
std::this_thread::sleep_for(std::chrono::milliseconds(3)); // std::this_thread::sleep_for(std::chrono::milliseconds(3));
#endif //#endif
} // }
} }
} }
else else
#endif #endif
if (!scan_working_)
{ {
if (callback::close_ui) if (callback::close_ui)
callback::close_ui(UI_INDICATOR); callback::close_ui(UI_INDICATOR);

View File

@ -78,7 +78,8 @@ class scanner : public ISaneInvoker, virtual public refer
int thread_start(void); int thread_start(void);
std::unique_ptr<std::thread> thread_starting_; std::unique_ptr<std::thread> thread_starting_;
bool scan_working_ = false; std::unique_ptr<std::thread> thread_stop_; // stop阻塞到工作线程结束在结束回调后才返回。为不阻塞界面在线程中停止
bool scan_working_ = false; // start阻塞到有一张图片才返回如果第一张出现错误比如卡纸则回调先于start返回结束标志。该变量用于协调FINISH事件通知
template<class T> template<class T>