强制从主线程调用SetWinEventHook

时间:2014-02-27 22:19:51

标签: c# .net windows multithreading pinvoke

我正在尝试在新创建的进程上注册一些对象级WinEvents挂钩,但正如官方documentation所说:

  

对于上下文事件,事件在调用SetWinEventHook的同一线程上传递。

我的问题是我从主线程以外的线程调用SetWinEventHook,这将导致主线程中没有收到任何回调。我一直在寻找一种方法来触发(从另一个线程)主线程中的SetWinEventHook方法调用。知道它是一个控制台应用程序,Invoke和BeginInvoke解决方案不起作用。我也尝试过Events和EventHandler。

我希望我的问题不会被误解。这是代码和输出。

// storing the callback as a field to make sure the garbage collector do not move it
// while being used in managed code, this is easier than using GCHandle 
static WinEventDelegate procDelegate = new Native.WinEventDelegate(WinEventProc);

static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, 
    int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
    {
        Console.WriteLine("callback, tid = " + Thread.CurrentThread.ManagedThreadId);
    }

ManagementEventWatcher processStartEvent = new ManagementEventWatcher("SELECT * FROM Win32_ProcessStartTrace");

void processStartEvent_EventArrived(object sender, EventArrivedEventArgs e)
{
    string processName = e.NewEvent.Properties["ProcessName"].Value.ToString();
    int processID = Convert.ToInt32(e.NewEvent.Properties["ProcessID"].Value);
    Process process = Process.GetProcessById(processID);
    IntPtr hook = SetWinEventHook(eventId, eventId, IntPtr.Zero, procDelegate, (uint)process.Id, 
            GetWindowThreadProcessId(process.MainWindowHandle, IntPtr.Zero),
            Native.WINEVENT_OUTOFCONTEXT | Native.WINEVENT_SKIPOWNPROCESS | Native.WINEVENT_SKIPOWNTHREAD); 
    if (hook == IntPtr.Zero)  Console.WriteLine("Hooking failed");
    Console.WriteLine("event, tid = " + Thread.CurrentThread.ManagedThreadId);
}

void watchProcess()
{
    processStartEvent.EventArrived += new EventArrivedEventHandler(processStartEvent_EventArrived);
    processStartEvent.Start();
}

static void Main(string[] args)
{
        Console.WriteLine("main, tid = " + Thread.CurrentThread.ManagedThreadId);
        watchProcess(); 
        MessageBox.Show("Message loop");          
}

输出:

  

main,tid = 1
  event,tid = 4 //重复创建新进程的次数

1 个答案:

答案 0 :(得分:0)

遇到了同样的问题。使用任何形式的Invoke方法:

            this.Invoke(new MethodInvoker(() => StartEventListener()));

此致