home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / thread / starting / starting.cpp < prev    next >
Text File  |  1996-10-29  |  7KB  |  209 lines

  1. /***************************************************************
  2. * FILE NAME: starting.cpp                                      *
  3. *                                                              *
  4. * DESCRIPTION:                                                 *
  5. *   This file contains a sample program that demonstrates      *
  6. *   all the ways in which you can start threads using IThread. *                              
  7. *                                                              *
  8. * COPYRIGHT:                                                   *
  9. *   Licensed Materials - Property of Solution Frameworks       *
  10. *   Copyright (C) 1996, Solution Frameworks                    *
  11. *   All Rights Reserved                                        *
  12. ***************************************************************/
  13. #ifdef __OS2__
  14. #define INCL_DOSPROCESS
  15. #include <os2.h>
  16. #else
  17. #include <windows.h>
  18. #include "wrapper.hpp"
  19. #endif
  20.  
  21. #include <ithread.hpp>
  22. #include <iframe.hpp>
  23. #include <ipushbut.hpp>
  24. #include <imsgbox.hpp>
  25. #include <istring.hpp>
  26. #include <icmdhdr.hpp>
  27.  
  28. static const IMessageBox::Style
  29.   mbStyle = IMessageBox::moveable | IMessageBox::okButton;
  30.  
  31. /*--------------------- compatibleWithOS -----------------------
  32. | This function is compatible with the operating               |
  33. | system functions DosCreateThread (on OS/2) or                |
  34. | CreateThread (on Windows).                                   |
  35. --------------------------------------------------------------*/
  36. #ifdef IC_PM
  37. void _System compatibleWithOS( unsigned long arg ) {
  38. #define RETURN return
  39. #else
  40. unsigned long _stdcall compatibleWithOS( void *arg ) {
  41. #define RETURN return 0
  42. #endif
  43.  
  44.   const char
  45.    *text = (const char *)arg;
  46.  
  47.   IMessageBox
  48.     msgBox( IWindow::desktopWindow() );
  49.  
  50.   IString
  51.     title = IString( "compatibleWithOS on thread " ) +
  52.               IThread::current().id().asString();
  53.  
  54.   msgBox
  55.     .setTitle( title )
  56.     .show( text, mbStyle );
  57.  
  58.   RETURN;
  59. }
  60.  
  61. /*------------------ compatibleWithCLibrary --------------------
  62. | This function is compatible with the C library function      |
  63. | _beginthread .                                               |
  64. ---------------------------------------------------------------*/
  65. void _Optlink compatibleWithCLibrary( void *arg ) {
  66.   const char
  67.    *text = (const char *)arg;
  68.  
  69.   IMessageBox
  70.     msgBox( IWindow::desktopWindow() );
  71.  
  72.   IString
  73.     title = IString( "compatibleWithCLibrary on thread " ) +
  74.               IThread::current().id().asString();
  75.  
  76.   msgBox
  77.     .setTitle( title )
  78.     .show( text, mbStyle );
  79. }
  80.  
  81. enum { createThread = 1,
  82.        beginthread, 
  83.        createThreadUsingIThreadStart,
  84.        createThreadUsingIThreadCtor,
  85.        beginThreadUsingIThreadStart,
  86.        beginThreadUsingIThreadCtor };
  87.  
  88. /*---------------------- ThreadStarter -------------------------
  89. | This class is a command handler that handles the starting of |
  90. | threads using any of the six different methods.              |
  91. --------------------------------------------------------------*/
  92. class ThreadStarter : public ICommandHandler {
  93. public:
  94. virtual Boolean
  95.   command ( ICommandEvent &event ) {
  96.     switch ( event.commandId() ) {
  97.       case createThread:
  98.         #ifdef IC_PM
  99.           TID
  100.             tid;
  101.           DosCreateThread( &tid, 
  102.                            compatibleWithOS,
  103.                            (unsigned long)"Started via DosCreateThread",
  104.                            0,
  105.                            0x4000 );
  106.         #else
  107.           DWORD
  108.             tid;
  109.           CreateThread( 0, 
  110.                         0x4000, 
  111.                         compatibleWithOS,
  112.                         (void *)"Started via CreateThread",
  113.                         0,
  114.                         &tid );
  115.         #endif
  116.         break;
  117.       case beginthread:
  118.         _beginthread( compatibleWithCLibrary,
  119.                       0,
  120.                       0x4000,
  121.                       (void *)"Started via _beginthread" );
  122.         break;
  123.       case createThreadUsingIThreadStart:
  124.         {
  125.         IThread
  126.           newThread;
  127.         #ifdef IC_PM
  128.           newThread.start( compatibleWithOS, 
  129.                            (unsigned long)"Started via IThread::start" );
  130.         #else
  131.           newThread.start( WrapperFor< compatibleWithOS >(),
  132.                            (void *)"Started via IThread::start" );
  133.         #endif
  134.         }
  135.         break;
  136.       case createThreadUsingIThreadCtor:
  137.         {
  138.         #ifdef IC_PM
  139.           IThread
  140.             newThread( compatibleWithOS,
  141.                        (unsigned long)"Started via IThread ctor" );
  142.         #else
  143.           IThread
  144.             newThread( WrapperFor< compatibleWithOS >(),
  145.                        (void *)"Started via IThread ctor" );
  146.         #endif
  147.         }
  148.         break;
  149.       case beginThreadUsingIThreadStart:
  150.         {
  151.         IThread
  152.           newThread;
  153.         newThread.start( compatibleWithCLibrary,
  154.                          (void*)"Started via IThread::start" );
  155.         }
  156.         break;
  157.       case beginThreadUsingIThreadCtor:
  158.         {
  159.         IThread
  160.           newThread( compatibleWithCLibrary,
  161.                      (void*)"Started via IThread ctor" );
  162.         }
  163.         break;
  164.     }
  165.     return false;
  166.   }
  167. }; // ThreadStarter
  168.  
  169. void main() {
  170.   IFrameWindow
  171.     frame( "Starting Threads" );
  172.  
  173.   IPushButton
  174.     button1( createThread, &frame, &frame ),
  175.     button2( beginthread, &frame, &frame ),
  176.     button3( createThreadUsingIThreadStart, &frame, &frame ),
  177.     button4( createThreadUsingIThreadCtor, &frame, &frame ),
  178.     button5( beginThreadUsingIThreadStart, &frame, &frame ),
  179.     button6( beginThreadUsingIThreadCtor, &frame, &frame );
  180.  
  181.   #ifdef IC_PM
  182.     button1.setText( "compatibleWithOS via DosCreateThread" );
  183.   #else
  184.     button1.setText( "compatibleWithOS via CreateThread" );
  185.   #endif
  186.   button2.setText( "compatibleWithCLibrary via _beginthread" );
  187.   button3.setText( "compatibleWithOS via IThread::start" );
  188.   button4.setText( "compatibleWithOS via IThread ctor" );
  189.   button5.setText( "compatibleWithCLibrary via IThread::start" );
  190.   button6.setText( "compatibleWithCLibrary via IThread ctor" );
  191.  
  192.   frame
  193.     .addExtension( &button1, IFrameWindow::aboveClient, 0.17 )
  194.     .addExtension( &button2, IFrameWindow::aboveClient, 0.20 )
  195.     .addExtension( &button3, IFrameWindow::aboveClient, 0.25 )
  196.     .addExtension( &button4, IFrameWindow::aboveClient, 0.33 )
  197.     .addExtension( &button5, IFrameWindow::aboveClient, 0.5 )
  198.     .addExtension( &button6, IFrameWindow::aboveClient, 0.99 )
  199.     .moveSizeTo  ( frame.rect().scaleBy( 0.5 ) )
  200.     .setFocus();
  201.  
  202.   ThreadStarter
  203.     handler;
  204.  
  205.   handler.handleEventsFor( &frame );
  206.  
  207.   frame.showModally();
  208. }
  209.