home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP14 / BIGJOB2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  5.0 KB  |  175 lines

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