0

操作系统:win7 32位

.net:3.5

我使用下面的函数(在线程中运行)来处理作业事件,一切正常。

private void JobEventThread()
{
    uint jobEvent = 0;
    UIntPtr comKey;
    IntPtr Overlapped;
    bool bGoOn = true;
    while (bGoOn 
        && Kernel32Dll.GetQueuedCompletionStatus
                (m_hiocp, out jobEvent, out comKey, out Overlapped, Win32Define.INFINITE))
    {
        if (comKey.ToUInt32() == completeKey)
        {
            switch (jobEvent)
            {
                case Win32Define.JOB_OBJECT_MSG_NEW_PROCESS:
                    int pid = Overlapped.ToInt32();
                    if (NewProcessEvent != null){NewProcessEvent(pid, GetProcessNameById(pid));}
                    break;

                case Win32Define.JOB_OBJECT_MSG_EXIT_PROCESS:
                    if (ExitProcessEvent != null) { ExitProcessEvent(Overlapped.ToInt32()); }
                    break;

                case Win32Define.JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO:
                    if (ExitJobEvent != null) { ExitJobEvent(); }
                    bGoOn = false;
                    break;
            }
        }
    }
}

但我尝试 DispatchTimer 做这样的事情,GetQueuedCompletionStatus 总是返回 false。

private void JobEventThread(object sender, EventArgs e)
{
    uint jobEvent = 0;
    UIntPtr comKey;
    IntPtr Overlapped;
    if (Kernel32Dll.GetQueuedCompletionStatus
                (m_hiocp, out jobEvent, out comKey, out Overlapped, 0))
    {
        if (comKey.ToUInt32() == completeKey)
        {
            switch (jobEvent)
            {
                case Win32Define.JOB_OBJECT_MSG_NEW_PROCESS:
                    int pid = Overlapped.ToInt32();
                    if (NewProcessEvent != null){NewProcessEvent(pid, GetProcessNameById(pid));}
                    break;

                case Win32Define.JOB_OBJECT_MSG_EXIT_PROCESS:
                    if (ExitProcessEvent != null) { ExitProcessEvent(Overlapped.ToInt32()); }
                    break;

                case Win32Define.JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO:
                    if (ExitJobEvent != null) { ExitJobEvent(); }
                    break;

                default:
                    break;
            }
        }
    }
}

我不希望我的事件来自线程,这会让我的程序变得复杂!

4

0 回答 0