停止/启动后台线程中的窗口挂钩
Stopping/Starting windows hook in background thread
我正试图安装一个在后台线程中运行的windows挂钩,直到它被用户通过调用cancelRun()
停止。
然而,当在cancelRun()
之后调用installHook()
时,我会得到一个"abort has called"异常。
标题:
#pragma once
#include <iostream>
#include <thread>
#include <Windows.h>
#pragma comment(lib, "user32.lib")
class GestureEngine{
private:
static HHOOK hHook_;
std::thread thread_;
bool cancelRun_ = false;
public:
GestureEngine(){
}
static GestureEngine& instance();
static inline LRESULT CALLBACK mouseProc(const int nCode, const WPARAM wParam, const LPARAM lParam)
{
// ....
return CallNextHookEx(hHook_, nCode, wParam, lParam);
}
void threadproc();
void installHook();
void cancelRun();
};
代码:
#include "GestureEngine.h"
HHOOK GestureEngine::hHook_ = NULL;
GestureEngine& GestureEngine::instance(){
static GestureEngine s_instance;
return s_instance;
}
void GestureEngine::installHook(){
thread_ = std::thread(&GestureEngine::threadproc, this);
cancelRun_ = false;
}
void GestureEngine::cancelRun(){
if (cancelRun_ == false) {
cancelRun_ = true;
thread_.join();
thread_.detach();
UnhookWindowsHookEx(hHook_);
cancelRun_ = false;
}
}
void GestureEngine::threadproc(){
MSG msg;
hHook_= SetWindowsHookEx( WH_MOUSE_LL, mouseProc, NULL, NULL );
while(!cancelRun_){
GetMessage(&msg, NULL, 0, 0);
TranslateMessage(&msg);
DispatchMessage(&msg);
};
return;
}
在installHook()
方法中执行行thread_ = std::thread(&GestureEngine::threadproc, this);
时发生错误
我看到函数'threadproc'是class GestureEngine的成员,也许你可以尝试将其设置为全局成员或静态函数。
相关文章:
- 自 Windows 10 20H1 以来,具有单独线程的多个窗口停止工作
- 如何从单独的线程控制 SFML 窗口?
- DirectX 窗口通过多窗口和多线程快速闪烁
- 如何将C++ dll 在 C# 窗口窗体应用程序下的工作线程中运行
- 在工作线程中使用 QT 主窗口
- 是否可以从其他线程隐藏qt小部件窗口
- 是否可以使用标准的C 线程而不是FLTK超时来更新窗口
- C++从异步线程更新窗口
- 将工作线程信号与主窗口插槽 (Qt5) 连接时出错
- 使用第二个线程快速更新 GUI 窗口会通过调整窗口大小或移动窗口而崩溃
- 将程序(.EXE)作为线程 - 窗口C 执行
- SDL2在不同线程中的两个窗口
- 2个线程 - 一个带有OpenGL窗口的线程,第二个带有WXWIDGETS的窗口
- SIGSEGV 信号处理程序不是从辅助线程 c++ 窗口调用的
- Qt 窗口包含提升线程标头失败
- 两个窗口-一个由线程随机输出修改
- 如果最初发出 IO 的线程在窗口 8 下的 ReadFile 中阻塞,则 GetQueuedCompletionStatus 无法将 IO 从 IOCP 中取消排队
- 关闭其他线程的对话框后无法从主窗口聚焦
- C++ 中窗口线程的异常输出
- 如何让窗口线程同时使用两个函数