- // MsgWaitForMultipleObjects.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <windows.h>
- #include <process.h>
- #include <stdio.h>
- #define WM_MM WM_USER + 100
- typedef struct _handltype
- {
- HANDLE ph;
- BOOL bValid;
- BOOL bThread;
- }HANDLETYPE;
- BOOL bTerm1 = FALSE;
- BOOL bTerm2 = FALSE;
- long cnt = 0;
- DWORD mainthreadid;
- HANDLE handles[3] = {0, 0, 0};
- unsigned long handlescnt = 0;
- HANDLETYPE handlestype[3];
- unsigned int WINAPI ThreadFun1(LPVOID pParam)
- {
- while(!bTerm1)
- {
- InterlockedExchangeAdd(&cnt, 1);
- printf("thread[%ld] :%ld!\n", GetCurrentThreadId(), cnt);
- Sleep(100);
- }
-
- return 0;
- }
- unsigned int WINAPI ThreadFun2(LPVOID pParam)
- {
- while(!bTerm2)
- {
- PostThreadMessage(mainthreadid, WM_MM, 0, 0);
- Sleep(100);
- }
-
- return 0;
- }
- BOOL KeyEventProc(KEY_EVENT_RECORD ev)
- {
- switch(ev.uChar.AsciiChar)
- {
- case 'a':
- bTerm1 = TRUE;
- break;
- case 'b':
- bTerm2 = TRUE;
- break;
-
- case 'c':
- bTerm1 = TRUE;
- bTerm2 = TRUE;
- return FALSE;
- default:
- break;
- }
- return TRUE;
- }
- BOOL dealconsole(HANDLE hStdin)
- {
- DWORD cNumRead;
- INPUT_RECORD irInBuf[128];
- unsigned int i = 0;
- if (! ReadConsoleInput(
- hStdin, // input buffer handle
- irInBuf, // buffer to read into
- 128, // size of read buffer
- &cNumRead) ) // number of records read
- {
- printf("ReadConsoleInput fail\n");
- return FALSE;
- }
-
-
- for (i = 0; i < cNumRead; i++)
- {
- switch(irInBuf[i].EventType)
- {
- case KEY_EVENT: // keyboard input
- return KeyEventProc(irInBuf[i].Event.KeyEvent);
- break;
-
- case MOUSE_EVENT: // mouse input
- //MouseEventProc(irInBuf[i].Event.MouseEvent);
- break;
-
- case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing
- //ResizeEventProc(irInBuf[i].Event.WindowBufferSizeEvent);
- break;
-
- case FOCUS_EVENT:
- case MENU_EVENT:
- break;
-
- default:
- printf("unkown console evnt!");
- break;
- }
- }
- return TRUE;
- }
- DWORD packHandleArray(HANDLETYPE * ph, int phlen, HANDLE* handles)
- {
- DWORD len = 0;
- int i;
- HANDLETYPE * phCur = ph;
- for(i = 0; i < phlen; i++, phCur++)
- {
- if(phCur->bValid == TRUE)
- {
- handles[len] = phCur->ph;
- len++;
- }
- }
- return len;
- }
- int main(int argc, char* argv[])
- {
- HANDLE hThread1, hThread2;
- unsigned threadID1, threadID2;
- HANDLE hStdin;
-
- DWORD result ;
- MSG msg ;
- BOOL running = TRUE;
- DWORD exitcode;
-
- hStdin = GetStdHandle(STD_INPUT_HANDLE);
- if (hStdin == INVALID_HANDLE_VALUE)
- {
- return 0;
- }
- if (! SetConsoleMode(hStdin, ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT))
- {
- return 0;
- }
- mainthreadid = GetCurrentThreadId();
- hThread1 = (HANDLE)_beginthreadex( NULL, 0, &ThreadFun1, NULL, 0, &threadID1 );
- handlestype[0].ph = hThread1;
- handlestype[0].bThread = TRUE;
- handlestype[0].bValid = TRUE;
- hThread2 = (HANDLE)_beginthreadex( NULL, 0, &ThreadFun2, NULL, 0, &threadID2 );
- handlestype[1].ph = hThread2;
- handlestype[1].bThread = TRUE;
- handlestype[1].bValid = TRUE;
- handlestype[2].ph = hStdin;
- handlestype[2].bThread = FALSE;
- handlestype[2].bValid = TRUE;
-
- handles[0] = hThread1;
- handles[1] = hThread2;
- handles[2] = hStdin;
- handlescnt = 3;
- while(running)
- {
- result = MsgWaitForMultipleObjects(handlescnt, handles, FALSE, INFINITE, QS_ALLEVENTS);
- if (result == (WAIT_OBJECT_0 + handlescnt))
- {
- if(GetMessage(&msg,NULL,0,0))
- {
- switch(msg.message)
- {
- case WM_MM:
- InterlockedExchangeAdd(&cnt, 1);
- printf("thread[%ld] :%ld!\n", mainthreadid, cnt);
- break;
- default:
- break;
- }
- }
- continue;
- }
- else
- {
- int i;
- for(i = 0; i < 3;i++)
- {
- if(handlestype[i].ph == handles[result - WAIT_OBJECT_0])
- {
- if(handlestype[i].bThread == TRUE)
- {
- GetExitCodeThread(handles[result - WAIT_OBJECT_0], &exitcode);
- if(exitcode == STILL_ACTIVE)
- {
- printf("thread slot %d is still running\n", i);
- }
- else
- {
- printf("thread slot %d is exit\n", i);
- }
- handlestype[i].bValid = FALSE;
- handlescnt = packHandleArray(handlestype, 3, handles);
- }
- else
- {
- running = dealconsole(hStdin);
- }
- break;
- }
- }
- }
- }
-
- printf("Press any key to exit!\n");
- getchar();
- return 0;
- }