home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / thread / simple / simple.cpp < prev   
C/C++ Source or Header  |  1996-10-29  |  5KB  |  148 lines

  1. #include <iframe.hpp>
  2. #include <istattxt.hpp>
  3. #include <ipushbut.hpp>
  4. #include <icmdhdr.hpp>
  5. #include <ithread.hpp>
  6. #include <istring.hpp>
  7.  
  8. enum { cmdThreaded, cmdNotThreaded, cmdDone };
  9.  
  10. static const char
  11.  *prompt = "Press a button to perform action";
  12.  
  13. /*-------------------------- Action ----------------------------
  14. | This simple class provides a single member function that     |
  15. | sleeps for 15 one-second intervals, updating a static        |
  16. | text window at each iteration.  At the end, a command        |
  17. | event is posted to a frame window.                           |
  18. --------------------------------------------------------------*/
  19. class Action {
  20. public:
  21.   Action ( IFrameWindow &frame,
  22.            IStaticText  &status )
  23.     : frame( frame ),
  24.       status( status )
  25.     {
  26.     }
  27. virtual void
  28.   performAction ( )
  29.     {
  30.     // Sleep for 15 seconds.
  31.     for ( int i = 15; i; i-- )
  32.       {
  33.       status
  34.         .setText( IString( i ) )
  35.         .refresh( IWindow::paintAllImmediate );
  36.       IThread::current().sleep( 1000 );
  37.       }
  38.     status
  39.       .setText( "" )
  40.       .refresh( IWindow::paintAllImmediate );
  41.     // Tell frame the action is "done."
  42.     frame.postEvent( IWindow::command, cmdDone );
  43.     }
  44. private:
  45. IFrameWindow
  46.  &frame;
  47. IStaticText
  48.  &status;
  49. Action ( const Action & );
  50. operator = ( const Action & );
  51. };
  52.  
  53. /*------------------------ CmdHandler --------------------------
  54. | This class is a simple command handler that processes        |
  55. | 3 separate commands:                                         |
  56. |   cmdNotThreaded - Invokes Action::performAction on the      |
  57. |                    current thread of execution               |
  58. |   cmdThreaded    - Runs Action::performAction on a           |
  59. |                    separate thread                           |
  60. |   cmdDone        - Handles completion of an action by        |
  61. |                    refreshing the frame window               |
  62. --------------------------------------------------------------*/
  63. class CmdHandler : public ICommandHandler {
  64. public:
  65.   // Handler attaches to frame + text.
  66.   CmdHandler ( IFrameWindow &frame,
  67.                IStaticText  &text,
  68.                IStaticText  &status )
  69.     : action( frame, status ),
  70.       frame( frame ),
  71.       text( text )
  72.     {
  73.     handleEventsFor( &frame );
  74.     }
  75. protected:
  76. virtual Boolean
  77.   command ( ICommandEvent &event )
  78.     {
  79.     switch ( event.commandId() )
  80.       {
  81.       case cmdThreaded:
  82.         {
  83.         frame.disable();
  84.         text.setText( "Performing action on separate thread" );
  85.         IThread
  86.           thread;
  87.         thread.start( new IThreadMemberFn<Action>
  88.                         ( action, Action::performAction ) );
  89.         break;
  90.         }
  91.       case cmdNotThreaded:
  92.         frame.disable();
  93.         text.setText( "Performing action on current thread" );
  94.         text.refresh( IWindow::paintAllImmediate );
  95.         action.performAction();
  96.         break;
  97.       case cmdDone:
  98.         frame.enable();
  99.         text.setText( prompt );
  100.         break;
  101.       }
  102.     return true;
  103.     }
  104. private:
  105. Action
  106.   action;
  107. IFrameWindow
  108.  &frame;
  109. IStaticText
  110.  &text;
  111. CmdHandler( const CmdHandler & );
  112. operator = ( const CmdHandler & );
  113. };
  114.  
  115. void main()
  116.   {
  117.   // Create the main window.
  118.   IFrameWindow
  119.     frame( "Multi-Threading Demo" );
  120.   // Use static text for client area and status window.
  121.   IStaticText
  122.     client( IC_FRAME_CLIENT_ID, &frame, &frame ),
  123.     status( 0, &frame, &frame );
  124.   // Create command handler to process button clicks.
  125.   CmdHandler
  126.     handler( frame, status, client );
  127.   client.setAlignment( IStaticText::centerCenter );
  128.   status.setText( prompt );
  129.   // Create buttons to trigger actions.
  130.   IPushButton
  131.     button1( cmdThreaded, &frame, &frame ),
  132.     button2( cmdNotThreaded, &frame, &frame );
  133.   button1.setText( "On another thread" );
  134.   button2.setText( "On current thread" );
  135.   // Put status window above client, buttons below.
  136.   frame.addExtension( &status, IFrameWindow::aboveClient );
  137.   frame.addExtension( &button1, IFrameWindow::belowClient );
  138.   frame.addExtension( &button2, IFrameWindow::belowClient );
  139.   frame.setClient( &client );
  140.   // Make frame window a more reasonable size.
  141.   frame.moveSizeTo( frame.rect().scaleBy(.5) );
  142.   // Show the main window.
  143.   frame.setFocus();
  144.   frame.show();          
  145.   // Process window events till user closes the main window.
  146.   IThread::current().processMsgs();
  147.   }
  148.