C#钩子函数放在线程里钩不上的解决办法
2013-04-19 17:07
423 查看
C#钩子函数放在线程里钩不上的解决办法
C#钩子函数如果直接用按钮引用,则没有问题,如果把这个函数放到多线程里,就会发现钩子钩不上了,这个问题我搞了一个星期也没搞定,后来还是查资料的时候无意中看到类似的问题,试了一下,发现真的可以完美解决此问题,现在把解决方法记下!解决方法:
只要在线程中加入下面这段代码就行了:
tagMSG Msgs;
while (GetMessage(out Msgs, IntPtr.Zero, 0, 0) > 0)
{
TranslateMessage(ref Msgs);
DispatchMessage(ref Msgs);
}
其中结构体和函数的定义如下:
[DllImport("user32", EntryPoint = "GetMessage")]
public static extern int GetMessage(
out tagMSG lpMsg,
IntPtr hwnd,
int wMsgFilterMin,
int wMsgFilterMax
);
[DllImport("user32", EntryPoint = "DispatchMessage")]
public static extern int DispatchMessage(
ref tagMSG lpMsg
);
[DllImport("user32", EntryPoint = "TranslateMessage")]
public static extern int TranslateMessage(
ref tagMSG lpMsg
);
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
}
public struct tagMSG
{
public int hwnd;
public uint message;
public int wParam;
public long lParam;
public uint time;
public int pt;
}
举个例子吧,下面1钩子不成功,2,3钩子成功:
1.(多线程失败)
private void Form1_Load(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(temp));
th.IsBackground = true;
//th.SetApartmentState(ApartmentState.STA);
th.Start();
}
private void temp()
{
hookKeyBoard = new HookKeyBoard();
hookKeyBoard.HookKeyBoardHandler += HookKeyBoardHandler;
hookKeyBoard.SetHook();
CheckForIllegalCrossThreadCalls = false;
}
2.(不利用多线程成功)
private void Form1_Load(object sender, EventArgs e)
{
hookKeyBoard = new HookKeyBoard();
hookKeyBoard.HookKeyBoardHandler += HookKeyBoardHandler;
hookKeyBoard.SetHook();
CheckForIllegalCrossThreadCalls = false;
}
3. (多线程成功)
private void Form1_Load(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(temp));
th.IsBackground = true;
//th.SetApartmentState(ApartmentState.STA);
th.Start();
}
private void temp()
{
hookKeyBoard = new HookKeyBoard();
hookKeyBoard.HookKeyBoardHandler += HookKeyBoardHandler;
hookKeyBoard.SetHook();
CheckForIllegalCrossThreadCalls = false;
tagMSG Msgs;
while (GetMessage(out Msgs, IntPtr.Zero, 0, 0) > 0)
{
TranslateMessage(ref Msgs);
DispatchMessage(ref Msgs);
}
}
写得够明白了吧,呵呵!
相关文章推荐
- liuxin-线程调用Updatedata函数出错的解决办法
- C#调用C(C++)dll示例教程,包含dll找不到函数入口的解决办法,包含C#dll调用方式不对出错的解决办法
- C#代码调用js函数,js函数中的document.getElementById("对象ID")得null值解决办法
- c#调用c++封装dll 参数为字符串输出函数的解决办法
- vue-cli项目在IE下运行钩子函数抛出异常“ReferenceError: “Promise”未定义"”的解决办法
- C#技巧【调用线程无法访问此对象,因为另一个线程拥有该对象的问题的解决办法】【C#读写EXCEL源码提示“office检测到此文件存在一个问题。为帮助保护您的计算机,不能打开此文件”的解决】
- C# List<> add函数总是覆盖List中原有元素的解决办法
- C# 线程间操作无效: 从不是创建控件...的解决办法
- C# List<> add函数总是覆盖List中原有元素的解决办法
- parameter 3 from 'unsigned long (void *)' to 'unsigned long (__stdcall *)VC以类的成员函数作为线程调用函数时易出错问题解决办法
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- C++ 11 线程调用类的成员函数解决办法
- Ogre线程渲染,渲染设备无法恢复窗口停止响应的案例及解决办法
- iOS -- 线程 还有待补全--(关于线程死锁,解决办法等。。)(NSThread、NSOperation、GCD) -- ASI/AFNetWork
- C# 线程中使用Timer,并向其触发的事件(函数)中传递参数
- vs2008调用opencv2.4.9的imread()函数失败解决办法
- PHP Json函数不能处理中文的解决办法
- ubuntu 9.04下open函数编译不过的解决办法
- PHP中file_exists()函数不能检测包含中文的文件名的解决办法
- C#编程 MWArray 报错 原因以及解决办法