home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / krcls012.zip / KrClass / source / krasyncm.cpp next >
C/C++ Source or Header  |  1997-02-14  |  4KB  |  113 lines

  1. // Kroni's Classes: Object for Asynchronous main function
  2. // (c) 1997 Wolfgang Kronberg
  3. // file: krasyncm.cpp
  4.  
  5.  
  6. #include "krasyncm.hpp"
  7. #include "krtrace.hpp"
  8.  
  9. #define INCL_DOSSEMAPHORES                       // DosCreateEventSem & Co
  10. #include <os2.h>
  11.  
  12. #include <iwindow.hpp>                           // IWindow
  13.  
  14.  
  15.  
  16. KrAsyncMain::KrAsyncMain (IWindow * mainWindow)
  17.   : ICommandHandler ()
  18.   , event (mainWindow,0,IEventParameter1(0),IEventParameter2(0))
  19.                                                  // Dummy value, to be overwritten later
  20.   , funcRef (this)
  21. {
  22.   window = mainWindow;
  23.   handleEventsFor (mainWindow);
  24.   threadRunning = false;
  25.   listStart = 0; listEnd = 0;
  26.  
  27.   HEV semHandle;
  28.   DosCreateEventSem (0,&semHandle,0,0);          // Create a private Semaphore, initially reset
  29.   sem = semHandle;
  30.  
  31.   thread.setPriority (IApplication::idleTime,0);
  32.   thread.start (funcRef,true);
  33.   window -> postEvent (IWindow::command, IEventParameter1(cmdStartUp));
  34. };
  35.  
  36.  
  37. Boolean KrAsyncMain::command (ICommandEvent & event)
  38. {
  39.   _KrEventList * p;
  40.  
  41.   if (threadRunning)                             // Can't execute this event directly
  42.      {
  43.      if (event.parameter1() == cmdFuncFinished)  // Thread has finished
  44.         {
  45.         if (listEnd)                             // Execute next waiting event
  46.            {
  47.            KrAsyncMain::event = *(listStart->event);
  48.            if (listStart==listEnd)               // This is the last element
  49.               {
  50.               listEnd = 0;
  51.               delete listStart->event;
  52.               delete listStart;
  53.               listStart= 0;
  54.               }
  55.            else                                  // There are still several events waiting
  56.               {
  57.               p = listStart->next;
  58.               delete listStart->event;
  59.               delete listStart;
  60.               listStart = p;
  61.               };
  62.            DosPostEventSem (sem);
  63.            }
  64.         else                                     // No events left, stop processing
  65.            {
  66.            threadRunning = false;                // In case this has not happened already anyway
  67.            }
  68.         }
  69.      else                                        // Thread still running, put event into waiting list
  70.         {
  71.         if (listEnd)                             // There are already elements in the list
  72.            {
  73.            listEnd->next = new _KrEventList;
  74.            listEnd = listEnd->next;
  75.            }
  76.         else                                     // The list is empty
  77.            {
  78.            listStart = new _KrEventList;
  79.            listEnd = listStart;
  80.            };
  81.         listEnd->event = new IEvent (event);
  82.         };
  83.      }
  84.   else                                           // Execute event directly
  85.      {
  86.      if (event.parameter1() == cmdFuncFinished)  // Thread has finished, this can't happen
  87.         _KRSYSTHROW (IInvalidRequest("More threads ended than begun"));
  88.      else                                        // Everything's OK, run the event directly
  89.         {
  90.         KrAsyncMain::event = event;
  91.         threadRunning = true;
  92.         DosPostEventSem (sem);
  93.         };
  94.      };
  95.   return true;
  96. };
  97.  
  98.  
  99. void KrAsyncMain::run ()
  100. {
  101.   unsigned long dummy;                           // needed for DosResetEventSem (number of posts to
  102.                                                  //   semaphore)
  103.   while (true)
  104.      {
  105.      DosWaitEventSem (sem,SEM_INDEFINITE_WAIT);
  106.      DosResetEventSem (sem,&dummy);
  107.      main (event);
  108.      window -> postEvent (IWindow::command, IEventParameter1(cmdFuncFinished));
  109.      }
  110. };
  111.  
  112.  
  113.