home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap20 / BigJob2 / BigJob2.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  5.0 KB  |  175 lines

  1. /*----------------------------------------
  2.    BIGJOB2.C -- Multithreading Demo
  3.                 (c) Charles Petzold, 1998
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <math.h>
  8. #include <process.h>
  9.  
  10. #define REP              1000000
  11.  
  12. #define STATUS_READY     0
  13. #define STATUS_WORKING   1
  14. #define STATUS_DONE      2
  15.  
  16. #define WM_CALC_DONE     (WM_USER + 0)
  17. #define WM_CALC_ABORTED  (WM_USER + 1)
  18.  
  19. typedef struct
  20. {
  21.      HWND   hwnd ;
  22.      HANDLE hEvent ;
  23.      BOOL   bContinue ;
  24. }
  25. PARAMS, *PPARAMS ;
  26.  
  27. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  28.  
  29. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  30.                     PSTR szCmdLine, int iCmdShow)
  31. {
  32.      static TCHAR szAppName[] = TEXT ("BigJob2") ;
  33.      HWND         hwnd ;
  34.      MSG          msg ;
  35.      WNDCLASS     wndclass ;
  36.  
  37.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  38.      wndclass.lpfnWndProc   = WndProc ;
  39.      wndclass.cbClsExtra    = 0 ;
  40.      wndclass.cbWndExtra    = 0 ;
  41.      wndclass.hInstance     = hInstance ;
  42.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  43.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  44.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  45.      wndclass.lpszMenuName  = NULL ;
  46.      wndclass.lpszClassName = szAppName ;
  47.      
  48.      if (!RegisterClass (&wndclass))
  49.      {
  50.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  51.                       szAppName, MB_ICONERROR) ;
  52.           return 0 ;
  53.      }
  54.      
  55.      hwnd = CreateWindow (szAppName, TEXT ("Multithreading Demo"),
  56.                           WS_OVERLAPPEDWINDOW,
  57.                           CW_USEDEFAULT, CW_USEDEFAULT,
  58.                           CW_USEDEFAULT, CW_USEDEFAULT,
  59.                           NULL, NULL, hInstance, NULL) ;
  60.      
  61.      ShowWindow (hwnd, iCmdShow) ;
  62.      UpdateWindow (hwnd) ;
  63.      
  64.      while (GetMessage (&msg, NULL, 0, 0))
  65.      {
  66.           TranslateMessage (&msg) ;
  67.           DispatchMessage (&msg) ;
  68.      }
  69.      return msg.wParam ;
  70. }
  71.  
  72. void Thread (PVOID pvoid)
  73. {
  74.      double           A = 1.0 ;
  75.      INT              i ;
  76.      LONG             lTime ;
  77.      volatile PPARAMS pparams ;
  78.      
  79.      pparams = (PPARAMS) pvoid ;
  80.      
  81.      while (TRUE)
  82.      {
  83.           WaitForSingleObject (pparams->hEvent, INFINITE) ;
  84.           
  85.           lTime = GetCurrentTime () ;
  86.           
  87.           for (i = 0 ; i < REP && pparams->bContinue ; i++)
  88.                A = tan (atan (exp (log (sqrt (A * A))))) + 1.0 ;
  89.           
  90.           if (i == REP)
  91.           {
  92.                lTime = GetCurrentTime () - lTime ;
  93.                PostMessage (pparams->hwnd, WM_CALC_DONE, 0, lTime) ;
  94.           }
  95.           else
  96.                PostMessage (pparams->hwnd, WM_CALC_ABORTED, 0, 0) ;
  97.      }
  98. }
  99.  
  100. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  101. {
  102.      static HANDLE  hEvent ;
  103.      static INT     iStatus ;
  104.      static LONG    lTime ;
  105.      static PARAMS  params ;
  106.      static TCHAR * szMessage[] = { TEXT ("Ready (left mouse button begins)"),
  107.                                     TEXT ("Working (right mouse button ends)"),
  108.                                     TEXT ("%d repetitions in %ld msec") } ;
  109.      HDC            hdc ;
  110.      PAINTSTRUCT    ps ;
  111.      RECT           rect ;
  112.      TCHAR          szBuffer[64] ;
  113.      
  114.      switch (message)
  115.      {
  116.      case WM_CREATE:
  117.           hEvent = CreateEvent (NULL, FALSE, FALSE, NULL) ;
  118.           
  119.           params.hwnd = hwnd ;
  120.           params.hEvent = hEvent ;
  121.           params.bContinue = FALSE ;
  122.           
  123.           _beginthread (Thread, 0, ¶ms) ;
  124.           
  125.           return 0 ;
  126.           
  127.      case WM_LBUTTONDOWN:
  128.           if (iStatus == STATUS_WORKING)
  129.           {
  130.                MessageBeep (0) ;
  131.                return 0 ;
  132.           }
  133.           
  134.           iStatus = STATUS_WORKING ;
  135.           params.bContinue = TRUE ;
  136.           
  137.           SetEvent (hEvent) ;
  138.         
  139.           InvalidateRect (hwnd, NULL, TRUE) ;
  140.           return 0 ;
  141.           
  142.      case WM_RBUTTONDOWN:
  143.           params.bContinue = FALSE ;
  144.           return 0 ;
  145.           
  146.      case WM_CALC_DONE:
  147.           lTime = lParam ;
  148.           iStatus = STATUS_DONE ;
  149.           InvalidateRect (hwnd, NULL, TRUE) ;
  150.           return 0 ;
  151.           
  152.      case WM_CALC_ABORTED:
  153.           iStatus = STATUS_READY ;
  154.           InvalidateRect (hwnd, NULL, TRUE) ;
  155.           return 0 ;
  156.           
  157.      case WM_PAINT:
  158.           hdc = BeginPaint (hwnd, &ps) ;
  159.           
  160.           GetClientRect (hwnd, &rect) ;
  161.           
  162.           wsprintf (szBuffer, szMessage[iStatus], REP, lTime) ;
  163.           DrawText (hdc, szBuffer, -1, &rect,
  164.                     DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
  165.           
  166.           EndPaint (hwnd, &ps) ;
  167.           return 0 ;
  168.           
  169.      case WM_DESTROY:
  170.           PostQuitMessage (0) ;
  171.           return 0 ;
  172.      }
  173.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  174. }
  175.