C#钩子函数放在线程里钩不上的解决办法
来源:互联网 发布:淘宝上的装修靠谱吗 编辑:程序博客网 时间:2024/06/02 03:13
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);
}
}
写得够明白了吧,呵呵!
- C#钩子函数放在线程里钩不上的解决办法
- 子线程钩子函数
- 系统钩子在C#的声明
- 放在函数后面的const是什么意思?
- const 放在函数前后的区别
- const放在函数前后的区别
- 钩子1(线程级的钩子)
- 在C#中使用钩子
- 在C#中使用钩子
- 在C#中使用针对当前程序的钩子
- C#什么是钩子,关于钩子的使用
- 当两个div无法放在一行的解决办法
- 钩子函数的学习
- 钩子函数的使用
- Vue的钩子函数
- C#钩子本线程内消息拦截
- C#钩子本线程内消息拦截
- C#钩子本线程内消息拦截
- POJ 1038 Bugs Integrated, Inc. 三进制压缩DP
- eclipse
- MFC 串口检测
- 多线程
- GoldenGate PUMP复制
- C#钩子函数放在线程里钩不上的解决办法
- 对自己这一段时间表现的反思
- Android应用开发之(小技巧之LocalBroadcastManager)
- Android TextView收缩
- 神州数码UDP数据包各种数据头标识
- qtopia-2.2.0 make overriding commands --- 问题解决
- 函数对象和函数指针
- TCP协议三次握手、四次挥手过程分析
- Javascript实用代码段-----纯Javascript写的飘动图片代码