home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / advanced / mtrecalc / calcthrd.cpp next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  2.4 KB  |  73 lines

  1. // calcthrd.cpp : implementation of the worker thread
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "calcthrd.h"
  15. #include "recaldoc.h"
  16. #include "slowcalc.h"
  17. #include "mtrecalc.h"
  18.  
  19. UINT RecalcThreadProc(LPVOID pParam)
  20. {
  21.     CRecalcThreadInfo* pRecalcInfo = (CRecalcThreadInfo*)pParam;
  22.  
  23.     // This recalc thread runs in an infinite loop, waiting to recalculate the
  24.     // result whenever the main application thread sets the "start recalc" event.
  25.     // The recalc thread exits the loop only when the main application sets the
  26.     // "kill recalc" event.
  27.  
  28.     BOOL bRecalcCompleted;
  29.     while (TRUE)
  30.     {
  31.         bRecalcCompleted = FALSE;
  32.  
  33.         // Wait until the main application thread asks this thread to do
  34.         //      another calculation.
  35.         if (WaitForSingleObject(pRecalcInfo->m_hEventStartRecalc, INFINITE)
  36.             != WAIT_OBJECT_0)
  37.             break;
  38.  
  39.         // Exit the thread if the main application sets the "kill recalc"
  40.         // event. The main application will set the "start recalc" event
  41.         // before setting the "kill recalc" event.
  42.  
  43.         if (WaitForSingleObject(pRecalcInfo->m_hEventKillRecalcThread, 0)
  44.             == WAIT_OBJECT_0)
  45.             break; // Terminate this thread by existing the proc.
  46.  
  47.         // Reset event to indicate "not done", that is, recalculation is in progress.
  48.         ResetEvent(pRecalcInfo->m_hEventRecalcDone);
  49.  
  50.         bRecalcCompleted = SlowAdd(pRecalcInfo->m_nInt1,
  51.             pRecalcInfo->m_nInt2,
  52.             pRecalcInfo->m_nSum,
  53.             pRecalcInfo,
  54.             pRecalcInfo->m_nRecalcSpeedSeconds,
  55.             pRecalcInfo->m_hwndNotifyProgress);
  56.  
  57.         // Set event to indicate that recalculation is done (i.e., no longer in progres),
  58.         // even if perhaps interrupted by "kill recalc" event detected in the SlowAdd function.
  59.         SetEvent(pRecalcInfo->m_hEventRecalcDone);
  60.  
  61.         if (!bRecalcCompleted)  // If interrupted by kill then...
  62.             break; // terminate this thread by exiting the proc.
  63.  
  64.         ::PostMessage(pRecalcInfo->m_hwndNotifyRecalcDone,
  65.             WM_USER_RECALC_DONE, 0, 0);
  66.     }
  67.  
  68.     if (!bRecalcCompleted)
  69.         SetEvent(pRecalcInfo->m_hEventRecalcThreadKilled);
  70.  
  71.     return 0;
  72. }
  73.