home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk11 / petzold / chap17 / bigjob4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-20  |  5.4 KB  |  158 lines

  1. /*---------------------------------------------------------------
  2.    BIGJOB4.C -- Second thread approach to lengthy processing job
  3.  ----------------------------------------------------------------*/
  4.  
  5. #define INCL_WIN
  6. #define INCL_DOS
  7. #include <os2.h>
  8. #include <mt\process.h>
  9. #include <mt\stdlib.h>
  10. #include "bigjob.h"
  11.  
  12. VOID _CDECL FAR CalcThread (PCALCPARAM) ;
  13.  
  14. HAB  hab ;
  15.  
  16. int main (void)
  17.      {
  18.      static CHAR  szClientClass [] = "BigJob4" ;
  19.      static ULONG flFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU  |
  20.                                  FCF_SIZEBORDER    | FCF_MINMAX   |
  21.                                  FCF_SHELLPOSITION | FCF_TASKLIST |
  22.                                  FCF_MENU ;
  23.      HMQ          hmq ;
  24.      HWND         hwndFrame, hwndClient ;
  25.      QMSG         qmsg ;
  26.  
  27.      hab = WinInitialize (0) ;
  28.      hmq = WinCreateMsgQueue (hab, 0) ;
  29.  
  30.      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
  31.  
  32.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  33.                                      &flFrameFlags, szClientClass,
  34.                                      " - A Second Thread",
  35.                                      0L, NULL, ID_RESOURCE, &hwndClient) ;
  36.  
  37.      WinSendMsg (hwndFrame, WM_SETICON,
  38.                  WinQuerySysPointer (HWND_DESKTOP, SPTR_APPICON, FALSE),
  39.                  NULL) ;
  40.  
  41.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  42.           WinDispatchMsg (hab, &qmsg) ;
  43.  
  44.      WinDestroyWindow (hwndFrame) ;
  45.      WinDestroyMsgQueue (hmq) ;
  46.      WinTerminate (hab) ;
  47.      return 0 ;
  48.      }
  49.     
  50. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  51.      {
  52.      static CALCPARAM cp ;
  53.      static LONG      lRepAmts [] = { 10, 100, 1000, 10000, 100000 } ;
  54.      static SHORT     sCurrentRep = IDM_10 ;
  55.      static SHORT     sStatus = STATUS_READY ;
  56.      static TID       tidCalc ;
  57.      static ULONG     ulElapsedTime ;
  58.      static VOID      *pThreadStack ;
  59.  
  60.      switch (msg)
  61.           {
  62.           case WM_COMMAND:
  63.                switch (COMMANDMSG(&msg)->cmd)
  64.                     {
  65.                     case IDM_10:
  66.                     case IDM_100:
  67.                     case IDM_1000:
  68.                     case IDM_10000:
  69.                     case IDM_100000:
  70.                          CheckMenuItem (hwnd, sCurrentRep, FALSE) ;
  71.                          sCurrentRep = COMMANDMSG(&msg)->cmd ;
  72.                          CheckMenuItem (hwnd, sCurrentRep, TRUE) ;
  73.                          return 0 ;
  74.  
  75.                     case IDM_START:
  76.                          if (NULL == (pThreadStack = malloc (STACKSIZE)))
  77.                               {
  78.                               WinAlarm (HWND_DESKTOP, WA_ERROR) ;
  79.                               return 0 ;
  80.                               }
  81.  
  82.                          cp.hwnd = hwnd ;
  83.                          cp.lCalcRep = lRepAmts [sCurrentRep - IDM_10] ;
  84.                          cp.fContinueCalc = TRUE ;
  85.  
  86.                          if (-1 == (tidCalc = _beginthread (CalcThread,
  87.                                         pThreadStack, STACKSIZE, &cp)))
  88.                               {
  89.                               free (pThreadStack) ;
  90.                               WinAlarm (HWND_DESKTOP, WA_ERROR) ;
  91.                               return 0 ;
  92.                               }
  93.  
  94.                          sStatus = STATUS_WORKING ;
  95.                          WinInvalidateRect (hwnd, NULL, FALSE) ;
  96.                          EnableMenuItem (hwnd, IDM_START, FALSE) ;
  97.                          EnableMenuItem (hwnd, IDM_ABORT, TRUE) ;
  98.                          return 0 ;
  99.  
  100.                     case IDM_ABORT:
  101.                          cp.fContinueCalc = FALSE ;
  102.                          EnableMenuItem (hwnd, IDM_ABORT, FALSE) ;
  103.                          return 0 ;
  104.                     }
  105.                break ;
  106.  
  107.           case WM_CALC_DONE:
  108.                sStatus = STATUS_DONE ;
  109.                ulElapsedTime = LONGFROMMP (mp1) ;
  110.                WinInvalidateRect (hwnd, NULL, FALSE) ;
  111.                EnableMenuItem (hwnd, IDM_START, TRUE) ;
  112.                EnableMenuItem (hwnd, IDM_ABORT, FALSE) ;
  113.                free (pThreadStack) ;
  114.                return 0 ;
  115.  
  116.           case WM_CALC_ABORTED:
  117.                sStatus = STATUS_READY ;
  118.                WinInvalidateRect (hwnd, NULL, FALSE) ;
  119.                EnableMenuItem (hwnd, IDM_START, TRUE) ;
  120.                free (pThreadStack) ;
  121.                return 0 ;
  122.  
  123.           case WM_PAINT:
  124.                PaintWindow (hwnd, sStatus, cp.lCalcRep, ulElapsedTime) ;
  125.                return 0 ;
  126.  
  127.           case WM_DESTROY:
  128.                if (sStatus = STATUS_WORKING)
  129.                     DosSuspendThread (tidCalc) ;
  130.                return 0 ;
  131.           }
  132.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  133.      }
  134.  
  135. VOID _CDECL FAR CalcThread (PCALCPARAM pcp)
  136.      {
  137.      double A ;
  138.      LONG   lRep, lTime ;
  139.  
  140.      lTime = WinGetCurrentTime (hab) ;
  141.  
  142.      for (A = 1.0, lRep = 0 ; lRep < pcp->lCalcRep &&
  143.                               pcp->fContinueCalc ; lRep++)
  144.           A = Savage (A) ;
  145.  
  146.      DosEnterCritSec () ;     // So thread is dead when message retrieved
  147.  
  148.      if (pcp->fContinueCalc)
  149.           {
  150.           lTime = WinGetCurrentTime (hab) - lTime ;
  151.           WinPostMsg (pcp->hwnd, WM_CALC_DONE, MPFROMLONG (lTime), NULL) ;
  152.           }
  153.      else
  154.           WinPostMsg (pcp->hwnd, WM_CALC_ABORTED, NULL, NULL) ;
  155.  
  156.      _endthread () ;
  157.      }
  158.