托管线程和非托管线程之间的差异是什么? [英] What is the deffierence betweent managed thread and unmanaged thread

查看:342
本文介绍了托管线程和非托管线程之间的差异是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况是,当我操作鼠标钩子时,我发现有两种类型的当前线程ID,代码如下:

Thread.CurrentThread.ManagedThreadId

AppDomain.GetCurrentThreadId()

ids彼此不同,为什么?



两次输入鼠标钩子程序,代码如下:

the situation is that,when I operate a mouse hook,I find that there are two types of current thread id ,codes below:
Thread.CurrentThread.ManagedThreadId
AppDomain.GetCurrentThreadId()
the ids are defferent with each other,why?

Entering the mouse hook procedure two times,codes below:

public partial class Form1 : Form
{
    MOUSEHOOK mouseHook = new MOUSEHOOK();
    MOUSEHOOK.HookHandle hookhandle = null;
    MOUSEHOOK.MouseHookStruct mhs = new MOUSEHOOK.MouseHookStruct();

    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        hookhandle = new MOUSEHOOK.HookHandle(HookProc);
        if (true == mouseHook.InstallHook(hookhandle))
        {
            Console.WriteLine("successed");
        }
        Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString());//10
        Console.WriteLine(AppDomain.GetCurrentThreadId().ToString());//2588
    }


    //hook procedure
    int HookProc(int nCode, int wParam, IntPtr lParam)
    {
      Marshal.PtrToStructure(lParam, mhs);
        switch (wParam)
        {
            case MOUSEHOOK.WM_LBUTTONUP:
                Console.WriteLine("how many times to enter?");//here print the sentence two times,
                break;
            default :
                break;
        }

        return MOUSEHOOK.CallNextHookEx(0, nCode, wParam, lParam);

    }



}
//hook class
public class MOUSEHOOK
{
    [DllImport("user32.dll")]
    private static extern int SetWindowsHookEx(int idHook, HookHandle HookProc, IntPtr hMod, int dwThreadId);
    //cancel
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    private static extern bool UnhookWindowsHookEx(int idHook);
    //call next hook proc
    [DllImport("user32.dll")]
    public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
    [DllImport("kernel32.dll")]
    public static extern int GetCurrentThreadId();

    [StructLayout(LayoutKind.Sequential)]
    public class POINT
    {
        public int x;
        public int y;
    }
    [StructLayout(LayoutKind.Sequential)]
    public class MouseHookStruct
    {
        public POINT pt;
        public int hwnd;
        public int wHitTestCode;
        public int dwExtraInfo;
    }

    public const int WH_MOUSE = 7;
    public const int WM_MOUSEMOVE = 0x200;
    public const int WM_LBUTTONDOWN = 0x201;
    public const int WM_LBUTTONUP = 0x202;
    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int WM_NCLBUTTONUP = 0xA2;
    public const int WM_NCMOUSEMOVE = 0xA0;
    public MouseHookStruct mhs = new MouseHookStruct();
    public int hhook;
    public delegate int HookHandle(int nCode, int wParam, IntPtr lParam);

    public bool InstallHook(HookHandle MouseProc)
    {
        hhook = SetWindowsHookEx(WH_MOUSE, MouseProc, IntPtr.Zero, GetCurrentThreadId());//AppDomain.GetCurrentThreadId()
        if (hhook != 0)
            return true;
        return false;
    }
    public void UnInstallHook()
    {
        bool ret = false;
        if (hhook != 0)
            ret = UnhookWindowsHookEx(hhook);
        if (ret)
            hhook = 0;
    }
}

推荐答案

检查AppDomain.GetCurrentThreadId Depreciated [ ^ ]和

替换AppDomain.GetCurrentThreadId();使用ManagedThreadId [ ^ ]



如果你想获得一个线程的id传递给unmanaged,请调用GetCurrentThreadId(),它将绕过obsoletion警告。

check AppDomain.GetCurrentThreadId Depreciated[^] and
Replacing AppDomain.GetCurrentThreadId(); with ManagedThreadId[^]

if you want to get a id for a thread to pass to unmanaged, pinvoke GetCurrentThreadId() and it will bypass the obsoletion warning.
[DllImport("kernel32.dll")]
public static extern int GetCurrentThreadId();


谢谢你,Damith,当我卸下鼠标时,我有一个疑问钩子,当我输入消息WM_LBUTTONUP时,钩子proc将被执行两次。然后我在SetWindowsHookEx函数中更改了第四个参数,但问题始终存在。
thank you ,Damith,I have a doubt that when I unstall a mouse hook,when I entercept the message WM_LBUTTONUP ,the hook proc will be executed two times . Then I change the forth parameter in the SetWindowsHookEx function,but the question exists always.


这篇关于托管线程和非托管线程之间的差异是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆