关闭设置界面后,启动一个线程延时1s,如果eventProcess没有处理,则再调用notifyClose; 日志添加进程PID信息

This commit is contained in:
gb 2023-05-25 15:37:29 +08:00
parent 5ac2aca692
commit 383fc01ed0
3 changed files with 63 additions and 59 deletions

View File

@ -3408,7 +3408,7 @@ void init_log(void)
hg_get_current_time_w(now);
g_log_level = get_log_level(g_path_file_.c_str());
swprintf_s(ts, _countof(ts) - 1, L"==================%s - %d==================\r\n", now, g_log_level);
swprintf_s(ts, _countof(ts) - 1, L"==================%s - %d, Proc %d==================\r\n", now, g_log_level, GetCurrentProcessId());
fwrite(ts, 2, lstrlenW(ts), g_file_);
}

View File

@ -776,7 +776,7 @@ static const SCANNERID scanner_guid = MAKE_SCANNER_ID(PRODUCT_PID, PRODUCT_VID);
static std::once_flag oc;
huagao_ds::huagao_ds() : cur_head_(NULL), dpi_(200), xfer_ready_failed_(false), log_all_triple_(false), scanner_status_(SCANNER_STATUS_NOT_INIT)
,count_(-1), bUiOnly_(false), is_getting_count_(false), show_setting_(false)
,count_(-1), bUiOnly_(false), show_setting_(false)
{
//std::call_once(oc, [&]() { log4cplus::Initializer(); });
}
@ -923,12 +923,11 @@ Result huagao_ds::eventProcess(const Identity&, Event& event)
notifyXferReady();
xfer_ready_ = false;
}
else*/ if (notfify_close_)
else*/ if (take_and_reset_notify_close_flag())
{
notifyCloseCancel();
notfify_close_ = false;
}
has_event_called_ = true;
event.setMessage(Msg::Null);
@ -951,7 +950,6 @@ Result huagao_ds::identityOpenDs(const Identity& id)
//
// return { ReturnCode::Failure, ConditionCode::CapBadOperation };
//}
is_getting_count_ = false;
if (!load_sane_util::is_ok())
load_sane_util::initialize(me_);
if (!load_sane_util::is_ok())
@ -987,13 +985,13 @@ Result huagao_ds::identityOpenDs(const Identity& id)
err = 0;
scanner_.reset(load_sane_util::open(scanner_guid, &err, attempt >= max_try));
}
if(attempt > 1)
load_sane_util::to_log(2, L"OpenDS: wait and try %d times.\r\n", attempt);
if (!scanner_.get())
{
//CloseHandle(singleton_);
//singleton_ = NULL;
wchar_t info[128] = { 0 };
swprintf_s(info, _countof(info) - 1, L"OpenDS error: %d\r\n", err);
load_sane_util::log_info(info, 2);
load_sane_util::to_log(3, L"OpenDS(%04x) error: %d\r\n", PRODUCT_PID, err);
load_sane_util::uninitialize();
return { ReturnCode::Failure, huagao_ds::condition_code_from_hg_error(err) };
@ -1006,6 +1004,7 @@ Result huagao_ds::identityOpenDs(const Identity& id)
memset(cur_head_, 0, sizeof(SANE_Parameters));
}
log_all_triple_ = get_config_number(L"twain-app", L"log-all-triple") == 1;
notify_close_ = get_config_number(L"twain-app", L"notify-close") == 1;
m_compression = Compression::None;
init_support_caps();
@ -1016,6 +1015,9 @@ Result huagao_ds::identityOpenDs(const Identity& id)
}
Result huagao_ds::identityCloseDs(const Identity&)
{
if (notify_close_thread_.get() && notify_close_thread_->joinable())
notify_close_thread_->join();
// ui_.reset();
if (scanner_.get())
{
@ -1116,7 +1118,6 @@ Result huagao_ds::userInterfaceEnable(const Identity&, UserInterface& ui)
notfify_close_ = false;
bUiOnly_ = false;
show_setting_ = false;
has_event_called_ = false;
if (!ui.showUi())
{
scanner_->ui_show_progress((HWND)ui.parent().raw(), m_bIndicator);
@ -1596,47 +1597,6 @@ Twpp::Result huagao_ds::setupFileXferReset(const Twpp::Identity& origin, Twpp::S
return badProtocol();
}
Result huagao_ds::call(const Identity& origin, DataGroup dg, Dat dat, Msg msg, void* data) {
static bool in_calling = false;
static unsigned nested_events = 0;
bool calling = in_calling;
//if (calling)
//{
// wchar_t buf[128] = { 0 }, dgs[20] = { 0 }, dts[20] = { 0 }, ms[20] = { 0 }, ss[20] = { 0 }, rcs[20] = { 0 }, cs[20] = { 0 };
//
// if (dg == DataGroup::Control && dat == Dat::Event && msg == Msg::ProcessEvent)
// {
// if (is_getting_count_ && scanner_status_ == SCANNER_STATUS_SCAN_1)
// {
// // let it go ...
// }
// else
// {
// if(nested_events == 0)
// load_sane_util::log_info(L"Nested Call: discard 'Msg::ProcessEvent'!\r\n", 0);
// nested_events++;
// return { ReturnCode::NotDsEvent, ConditionCode::Success };
// }
// }
// else
// {
// swprintf_s(buf, _countof(buf) - 1, L"Nested Call: [%x - %s]DSEntry(%s, %s, %s) called in another operation! \r\n", GetCurrentThreadId(), desc_state(state(), ss),
// desc_data_group(dg, dgs), desc_data(dat, dts), desc_msg(msg, ms));
// load_sane_util::log_info(buf, 0);
// }
//}
//else
//{
// in_calling = true;
//}
//if (nested_events > 1)
//{
// wchar_t buf[256] = { 0 };
// swprintf_s(buf, _countof(buf) - 1, L"Discard %u nested Msg::ProcessEvent messages before.\r\n", nested_events);
// load_sane_util::log_info(buf, 0);
//}
nested_events = 0;
try {
// we can override almost anything from SourceFromThis, even the top-most source instance call
//FileTools::write_log("D:\\1.txt", "call:datagroup-"+to_string((int)dg)+"dat-"+to_string(int(dat))+"msg-"+to_string(int(msg)));
@ -1684,13 +1644,11 @@ Result huagao_ds::call(const Identity& origin, DataGroup dg, Dat dat, Msg msg, v
}
}
in_calling = calling;
return rt;
}
catch (const CapabilityException& e) {
//FileTools::writelog(log_ERROR, e.what());
UNREFERENCED_PARAMETER(e);
in_calling = calling;
return badValue();
}
}
@ -3635,10 +3593,28 @@ int huagao_ds::handle_scanner_event(int ev, bool from_event_proc)
break;
case SANE_EVENT_UI_CLOSE_SETTING:
scanner_->ui_hide();
if(has_event_called_)
if (notify_close_ == NOTIFY_AUTO)
{
load_sane_util::to_log(1, L"close setting ui and notify close in auto mode.\r\n");
//if (has_event_called_)
// notfify_close_ = true;
//else
// rc = notifyCloseCancel();
notfify_close_ = true;
else
if (notify_close_thread_.get() && notify_close_thread_->joinable())
notify_close_thread_->join();
notify_close_thread_.reset(new std::thread(&huagao_ds::notify_close_thread, this));
}
else if (notify_close_ == NOTIFY_ALWAYS)
{
load_sane_util::to_log(1, L"close setting ui and notify close immediately.\r\n");
rc = notifyCloseCancel();
}
else if (notify_close_ == NOTIFY_NONE)
{
load_sane_util::to_log(1, L"close setting ui and notify close passively (wait eventProcess to do).\r\n");
notfify_close_ = true;
}
//if(main_thread_id_ == GetCurrentThreadId())
// rc = notifyCloseCancel();
//else
@ -3717,7 +3693,6 @@ int huagao_ds::handle_scanner_event(int ev, bool from_event_proc)
}
int huagao_ds::get_scanned_image_count(DWORD timeout)
{
is_getting_count_ = true;
int cnt = scanner_->get_scanned_images(timeout);
//if (cnt == -1)
@ -3727,7 +3702,6 @@ int huagao_ds::get_scanned_image_count(DWORD timeout)
// scanner_->ui_hide();
// notifyCloseCancel();
//}
is_getting_count_ = false;
return cnt;
}
@ -3789,3 +3763,27 @@ void huagao_ds::trigger_ProcessEvent(Twpp::DataGroup dg, Twpp::Dat dat, Twpp::Ms
}
}
bool huagao_ds::take_and_reset_notify_close_flag(void)
{
std::lock_guard<std::mutex> lock(notify_close_lock_);
bool notify = notfify_close_;
if (notfify_close_)
notfify_close_ = false;
return notify;
}
void huagao_ds::notify_close_thread(void)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
if (take_and_reset_notify_close_flag())
{
notifyCloseCancel();
load_sane_util::to_log(1, L"Trigger notifyCloseCancel in custom thread!\r\n");
}
else
{
load_sane_util::to_log(1, L"Good! Trigger notifyCloseCancel in eventProcess ^_^\r\n");
}
}

View File

@ -57,9 +57,15 @@ class huagao_ds : public Twpp::SourceFromThis<huagao_ds> {
bool bUiOnly_;
bool show_setting_;
int count_;
volatile bool is_getting_count_;
volatile bool notfify_close_ = false;
volatile bool has_event_called_ = false;
enum {NOTIFY_AUTO = 0, NOTIFY_ALWAYS, NOTIFY_NONE};
volatile unsigned notify_close_ = 0; // 0 - auto; 1 - notify always; 2 - no notify
std::mutex notify_close_lock_;
std::unique_ptr<std::thread> notify_close_thread_;
bool take_and_reset_notify_close_flag(void);
void notify_close_thread(void);
static std::string get_hidedlg_path(void);
static void showmsg(const char* msg, int err);
static int __stdcall on_scanner_event(int ev, void* param);