Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1844217
  • 博文数量: 274
  • 博客积分: 2366
  • 博客等级: 大尉
  • 技术积分: 1880
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-22 09:37
文章分类

全部博文(274)

文章存档

2022年(1)

2020年(10)

2019年(7)

2018年(18)

2017年(26)

2016年(32)

2015年(43)

2014年(30)

2013年(44)

2012年(36)

2011年(17)

2010年(10)

分类: C/C++

2011-04-18 10:43:24

 

  1. // MsgWaitForMultipleObjects.cpp : Defines the entry point for the console application.
  2. //

  3. #include "stdafx.h"
  4. #include <windows.h>
  5. #include <process.h>
  6. #include <stdio.h>

  7. #define WM_MM WM_USER + 100

  8. typedef struct _handltype
  9. {
  10.     HANDLE ph;
  11.     BOOL bValid;
  12.     BOOL bThread;
  13. }HANDLETYPE;

  14. BOOL bTerm1 = FALSE;
  15. BOOL bTerm2 = FALSE;
  16. long cnt = 0;
  17. DWORD mainthreadid;
  18. HANDLE handles[3] = {0, 0, 0};
  19. unsigned long handlescnt = 0;
  20. HANDLETYPE handlestype[3];

  21. unsigned int WINAPI ThreadFun1(LPVOID pParam)
  22. {
  23.     while(!bTerm1)
  24.     {
  25.         InterlockedExchangeAdd(&cnt, 1);
  26.         printf("thread[%ld] :%ld!\n", GetCurrentThreadId(), cnt);
  27.         Sleep(100);
  28.     }
  29.     
  30.     return 0;
  31. }

  32. unsigned int WINAPI ThreadFun2(LPVOID pParam)
  33. {
  34.     while(!bTerm2)
  35.     {
  36.         PostThreadMessage(mainthreadid, WM_MM, 0, 0);
  37.         Sleep(100);
  38.     }
  39.     
  40.     return 0;
  41. }

  42. BOOL KeyEventProc(KEY_EVENT_RECORD ev)
  43. {
  44.     switch(ev.uChar.AsciiChar)
  45.     {
  46.         case 'a':
  47.             bTerm1 = TRUE;
  48.             break;

  49.         case 'b':
  50.             bTerm2 = TRUE;
  51.             break;
  52.         
  53.         case 'c':
  54.             bTerm1 = TRUE;
  55.             bTerm2 = TRUE;
  56.             return FALSE;

  57.         default:
  58.             break;
  59.     }

  60.     return TRUE;
  61. }

  62. BOOL dealconsole(HANDLE hStdin)
  63. {
  64.     DWORD cNumRead;
  65.     INPUT_RECORD irInBuf[128];
  66.     unsigned int i = 0;


  67.     if (! ReadConsoleInput(
  68.         hStdin, // input buffer handle
  69.         irInBuf, // buffer to read into
  70.         128, // size of read buffer
  71.         &cNumRead) ) // number of records read
  72.     {
  73.         printf("ReadConsoleInput fail\n");
  74.         return FALSE;
  75.     }
  76.     
  77.     
  78.     for (i = 0; i < cNumRead; i++)
  79.     {
  80.         switch(irInBuf[i].EventType)
  81.         {
  82.             case KEY_EVENT: // keyboard input
  83.                 return KeyEventProc(irInBuf[i].Event.KeyEvent);
  84.                 break;
  85.                 
  86.             case MOUSE_EVENT: // mouse input
  87.                 //MouseEventProc(irInBuf[i].Event.MouseEvent);
  88.                 break;
  89.                 
  90.             case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing
  91.                 //ResizeEventProc(irInBuf[i].Event.WindowBufferSizeEvent);
  92.                 break;
  93.                 
  94.             case FOCUS_EVENT:                
  95.             case MENU_EVENT:
  96.                 break;
  97.                 
  98.             default:
  99.                 printf("unkown console evnt!");
  100.                 break;
  101.         }
  102.     }

  103.     return TRUE;
  104. }

  105. DWORD packHandleArray(HANDLETYPE * ph, int phlen, HANDLE* handles)
  106. {
  107.     DWORD len = 0;
  108.     int i;
  109.     HANDLETYPE * phCur = ph;

  110.     for(i = 0; i < phlen; i++, phCur++)
  111.     {
  112.         if(phCur->bValid == TRUE)
  113.         {
  114.             handles[len] = phCur->ph;
  115.             len++;
  116.         }
  117.     }

  118.     return len;
  119. }

  120. int main(int argc, char* argv[])
  121. {
  122.     HANDLE hThread1, hThread2;
  123.     unsigned threadID1, threadID2;
  124.     HANDLE hStdin;
  125.     
  126.     DWORD result ;
  127.     MSG msg ;
  128.     BOOL running = TRUE;
  129.     DWORD exitcode;

  130.     
  131.     hStdin = GetStdHandle(STD_INPUT_HANDLE);
  132.     if (hStdin == INVALID_HANDLE_VALUE)
  133.     {
  134.         return 0;
  135.     }

  136.     if (! SetConsoleMode(hStdin, ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT))
  137.     {
  138.         return 0;
  139.     }

  140.     mainthreadid = GetCurrentThreadId();

  141.     hThread1 = (HANDLE)_beginthreadex( NULL, 0, &ThreadFun1, NULL, 0, &threadID1 );
  142.     handlestype[0].ph = hThread1;
  143.     handlestype[0].bThread = TRUE;
  144.     handlestype[0].bValid = TRUE;

  145.     hThread2 = (HANDLE)_beginthreadex( NULL, 0, &ThreadFun2, NULL, 0, &threadID2 );
  146.     handlestype[1].ph = hThread2;
  147.     handlestype[1].bThread = TRUE;
  148.     handlestype[1].bValid = TRUE;

  149.     handlestype[2].ph = hStdin;
  150.     handlestype[2].bThread = FALSE;
  151.     handlestype[2].bValid = TRUE;
  152.     
  153.     handles[0] = hThread1;
  154.     handles[1] = hThread2;
  155.     handles[2] = hStdin;
  156.     handlescnt = 3;

  157.     while(running)
  158.     {
  159.         result = MsgWaitForMultipleObjects(handlescnt, handles, FALSE, INFINITE, QS_ALLEVENTS);         
  160.         if (result == (WAIT_OBJECT_0 + handlescnt))
  161.         {
  162.             if(GetMessage(&msg,NULL,0,0))
  163.             {
  164.                 switch(msg.message)
  165.                 {
  166.                     case WM_MM:                    
  167.                         InterlockedExchangeAdd(&cnt, 1);
  168.                         printf("thread[%ld] :%ld!\n", mainthreadid, cnt);
  169.                         break;

  170.                     default:
  171.                         break;
  172.                 }
  173.             }

  174.             continue;
  175.         }
  176.         else
  177.         {
  178.             int i;
  179.             for(i = 0; i < 3;i++)
  180.             {
  181.                 if(handlestype[i].ph == handles[result - WAIT_OBJECT_0])
  182.                 {
  183.                     if(handlestype[i].bThread == TRUE)
  184.                     {
  185.                         GetExitCodeThread(handles[result - WAIT_OBJECT_0], &exitcode);
  186.                         if(exitcode == STILL_ACTIVE)
  187.                         {
  188.                             printf("thread slot %d is still running\n", i);
  189.                         }
  190.                         else
  191.                         {
  192.                             printf("thread slot %d is exit\n", i);
  193.                         }

  194.                         handlestype[i].bValid = FALSE;
  195.                         handlescnt = packHandleArray(handlestype, 3, handles);
  196.                     }
  197.                     else
  198.                     {
  199.                         running = dealconsole(hStdin);
  200.                     }
  201.                     break;
  202.                 }
  203.             }
  204.         }
  205.     }


  206.     
  207.     printf("Press any key to exit!\n");
  208.     getchar();
  209.     return 0;
  210. }
阅读(1351) | 评论(0) | 转发(0) |
0

上一篇:一些感慨

下一篇:directshow avchat编译手记

给主人留下些什么吧!~~