From b1aebe87efafc659946179f6410afd9c240738f3 Mon Sep 17 00:00:00 2001 From: fxliang Date: Wed, 22 May 2024 18:07:57 +0800 Subject: [PATCH] avoid freezing if _SendMessage overtime when ClientImpl::ProcessKeyEvent --- WeaselIPC/WeaselClientImpl.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/WeaselIPC/WeaselClientImpl.cpp b/WeaselIPC/WeaselClientImpl.cpp index 26122ad50..b2d784f22 100644 --- a/WeaselIPC/WeaselClientImpl.cpp +++ b/WeaselIPC/WeaselClientImpl.cpp @@ -1,6 +1,8 @@ #include "stdafx.h" #include "WeaselClientImpl.h" #include +#include +#include using namespace weasel; @@ -59,9 +61,20 @@ bool ClientImpl::ProcessKeyEvent(KeyEvent const& keyEvent) { if (!_Active()) return false; - LRESULT ret = - _SendMessage(WEASEL_IPC_PROCESS_KEY_EVENT, keyEvent, session_id); - return ret != 0; + // create async task _SendMessage + auto future = std::async(std::launch::async, [this, &keyEvent]() { + return _SendMessage(WEASEL_IPC_PROCESS_KEY_EVENT, keyEvent, session_id); + }); + + // wait _SendMessage complete or overtime + if (future.wait_for(std::chrono::seconds(2)) == std::future_status::timeout) { + // _SendMessage overtime + return false; + } else { + // _SendMessage complete + LRESULT ret = future.get(); + return ret != 0; + } } bool ClientImpl::CommitComposition() {