Modification Procedures

The DoThread procedure responds to menu commands by modifying whichever thread is currently selected in the list box. DoThread can raise or lower a thread’s priority, and suspend or resume threads. The iState array records the current state, either active or suspended, of each thread. The hThreads array holds handles to each of the four secondary threads.

/*—————————————————————————————————————————————————————————————————————
   DO THREAD
      Modify a thread’s priority or change its state in response
      to commands from the menu.
—————————————————————————————————————————————————————————————————————*/
void DoThread( int iCmd )
{
   int iThread;
   int iPriority;
      // determine which thread to modify
   iThread = ListBox_GetCurSel( hwndList );
   switch( iCmd )
   {
      case IDM_SUSPEND:
            // if the thread is not suspended, then suspend it
         if( iState[iThread] != SUSPENDED )
         {
            SuspendThread( hThread[iThread] );
            iState[iThread] = SUSPENDED;
         }
         break;
      case IDM_RESUME:
            // if the thread is not active, then activate it
         if( iState[iThread] != ACTIVE )
         {
            ResumeThread( hThread[iThread] );
            iState[iThread] = ACTIVE;
         }
         break;
      case IDM_INCREASE:
            // increase the thread’s priority (unless it is
            // already at the highest level)
         iPriority = GetThreadPriority( hThread[iThread] );
         switch( iPriority )
         {
            case THREAD_PRIORITY_LOWEST:
               SetThreadPriority( hThread[iThread],
                                  THREAD_PRIORITY_BELOW_NORMAL );
               break;
            case THREAD_PRIORITY_BELOW_NORMAL:
               SetThreadPriority( hThread[iThread],
                                  THREAD_PRIORITY_NORMAL );
               break;
            case THREAD_PRIORITY_NORMAL:
               SetThreadPriority( hThread[iThread],
                                  THREAD_PRIORITY_ABOVE_NORMAL );
               break;
            case THREAD_PRIORITY_ABOVE_NORMAL:
               SetThreadPriority( hThread[iThread],
                                  THREAD_PRIORITY_HIGHEST );
               break;
            default:        break;
         }                  break;
      case IDM_DECREASE:
         // decrease the thread’s priority (unless it is
         // already at the lowest level)
         iPriority = GetThreadPriority( hThread[iThread] );
         switch( iPriority )
         {
            case THREAD_PRIORITY_BELOW_NORMAL:
               SetThreadPriority( hThread[iThread],
                                  THREAD_PRIORITY_LOWEST );
               break;
            case THREAD_PRIORITY_NORMAL:
               SetThreadPriority( hThread[iThread],
                                  THREAD_PRIORITY_BELOW_NORMAL );
               break;
            case THREAD_PRIORITY_ABOVE_NORMAL:
               SetThreadPriority( hThread[iThread],
                                  THREAD_PRIORITY_NORMAL );
               break;
            case THREAD_PRIORITY_HIGHEST:
               SetThreadPriority( hThread[iThread],
                                  THREAD_PRIORITY_ABOVE_NORMAL );
               break;
            default:        break;
         }                  break;
      default:              break;
   }
   return;
}

© 1998 SYBEX Inc. All rights reserved.