Create TOC

2007년 11월 18일

To create a notification thread and then return

int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) {

    // Create our events for event logging notification as well as our event for ending the notification thread
    g_evtNewEventLogRecord = CreateEvent(NULL, TRUE, FALSE, NULL);
    HANDLE hThread = chBEGINTHREADEX(NULL, 0, EventNotifyThread, NULL, 0, NULL);

    DialogBox(hinstExe, MAKEINTRESOURCE(IDD_EVENTMONITOR), NULL, Dlg_Proc);

    // Clean up
    QueueUserAPC(DoNothingAPC, hThread, NULL);
    WaitForSingleObject(hThread, INFINITE);
    CloseHandle(g_evtNewEventLogRecord);
    CloseHandle(hThread);
    return(0);
}

///////////////////////////////////////////////////////////////////////////////
DWORD WINAPI EventNotifyThread(PVOID pvParam) {

    // Note that this is not the thread that called NotifyChangeEventLog.
    // The main thread does this in response to the WM_INITDIALOG message.
    // If the main thread were to terminate before this thread, then
    // notifications would cease to function. However, since it will always
    // terminate after this thread, then we can count on the notification event
    // working properly.

    // Wait for an event notification or for APC altering this thread to termiante.
    while (WaitForSingleObjectEx(g_evtNewEventLogRecord, INFINITE, TRUE) != WAIT_IO_COMPLETION) {
        FORWARD_WM_USERNOTIFYUPDATE(g_hwnd, PostMessage);
    }

    // We got an APC, this thread should terminate
    return(0);
}