home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pwr16.zip / TEST.C < prev    next >
C/C++ Source or Header  |  1994-11-07  |  12KB  |  248 lines

  1. /*______________________________________________________________________
  2. |                PowerThread  (c) 1994 Greg Ratajik                     |
  3. |-----------------------------------------------------------------------|
  4. | Description:  This module allows testing of the PowerThread function  |
  5. |               calls.  See POWER.C or POWER.DOC for more information.  |
  6. |                                                                       |
  7. |   *****************************************************************   |
  8. |   *DISCLAIMER OF WARRANTIES.  The following  code is              *   |
  9. |   * sample code created by Greg Ratajik.  The code is provided    *   |
  10. |   *"AS IS", without warranty of any kind.  Greg Ratatjik shall not*   |
  11. |   *be liable for any damages arising out of your use of the sample*   |
  12. |   *code.                                                          *   |
  13. |   *****************************************************************   |
  14. |_____________________________________________________________________*/
  15. #define INCL_PM             /*  PM Support                           */
  16. #define INCL_DOSPROCESS     /*  Process and thread support           */
  17.  
  18. /*______________________________________________________________________
  19. |                                                                       |
  20. |                         System Includes                               |
  21. |_____________________________________________________________________*/
  22. #define _MT
  23.  
  24. #include <os2.h>            /* Base OS/2 Include                     */
  25. #include <stdlib.h>         /* malloc and free                       */
  26. #include <process.h>        /* _beginthread support                  */
  27.  
  28. /*______________________________________________________________________
  29. |                                                                       |
  30. |             Application Specific Includes & Defines                   |
  31. |_____________________________________________________________________*/
  32. #include "test.rch"  /* RC info for the DLG                    */
  33. #include "power.h"   /* Prototypes for PowerCalls              */
  34.  
  35. /*______________________________________________________________________
  36. |                                                                       |
  37. |  Gobal Local Variables                        |
  38. |_____________________________________________________________________*/
  39. HAB   hAB;
  40.  
  41. /*____________________________________________________________________
  42. |                                      |
  43. |  Function Prototypes.                           |
  44. |____________________________________________________________________*/
  45. MRESULT EXPENTRY AppWndProc(HWND hwndClient,USHORT ulMsg,MPARAM mpParm1, MPARAM mpParm2);
  46.  
  47. VOID _pascal far ProcessTest ( USHORT *);
  48.  
  49. /*___________________________________________________________________________
  50. |                                                                            |
  51. |    Function: Main                                 |
  52. |                                                                            |
  53. | Description: Main function                                                 |
  54. |                                         |
  55. |      Return: SHORT                                                         |
  56. |                                                                            |
  57. | -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-  |
  58. |                 C H A N G E    L O G                 |
  59. | -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-  |
  60. |                                                                            |
  61. |  PRG.    DATE       DESCRIPTION                                           |
  62. |  ----    ---------- ---------------------------------------------------   |
  63. |  RATAJIK 10-01-1994 Initial Developement                                  |
  64. |___________________________________________________________________________*/
  65. SHORT cdecl main(int argc, char *argv[])
  66. {
  67. HMQ hMQ;
  68.  
  69.  hAB = WinInitialize(0);                /* Init the process for PM         */
  70.  hMQ = WinCreateMsgQueue(hAB, 0);       /* Create a Message queue          */
  71.  
  72.  /*____________________________________
  73.  |                                     |
  74.  | Start the test DLG.                 |
  75.  |                                     |
  76.  |____________________________________*/
  77.  WinDlgBox(HWND_DESKTOP,
  78.            HWND_DESKTOP,
  79.            AppWndProc,
  80.            0,
  81.            DLG_APP_TEST,
  82.            (PVOID)NULL);
  83.  
  84. return(0);
  85. }
  86.  
  87. /*___________________________________________________________________________
  88. |                                                                            |
  89. |    Function: AppWndProc                                                    |
  90. |                                                                            |
  91. | Description: Test window proc to test PowerThreads                         |
  92. |                                         |
  93. |      Return: SHORT                                                         |
  94. |                                                                            |
  95. | -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-  |
  96. |                 C H A N G E    L O G                 |
  97. | -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-  |
  98. |                                                                            |
  99. |  PRG.    DATE       DESCRIPTION                                           |
  100. |  ----    ---------- ---------------------------------------------------   |
  101. |  RATAJIK 10-01-1994 Initial Developement                                  |
  102. |___________________________________________________________________________*/
  103. MRESULT EXPENTRY AppWndProc(HWND hWndDlg, USHORT msg, MPARAM mp1, MPARAM mp2)
  104.  
  105. {
  106.    USHORT usTotal;
  107.  
  108.    switch (msg)
  109.       {
  110.          case WM_INITDLG:
  111.               WinSetDlgItemText(hWndDlg, TX_INFO, POWER_INFO);
  112.               WinSetDlgItemText(hWndDlg, EF_NUMBER, "5");
  113.               break;
  114.  
  115.          case WM_CLOSE:
  116.               WinDismissDlg(hWndDlg, TRUE);
  117.               break;
  118.  
  119.          case WM_COMMAND:
  120.               switch (SHORT1FROMMP(mp1))
  121.                  {
  122.                   case PB_THREAD_1:
  123.                        WinQueryDlgItemShort(hWndDlg, EF_NUMBER, &usTotal, FALSE);
  124.  
  125.                        /*____________________________________
  126.                        |                                     |
  127.                        | Call the ProcessTest function,      |
  128.                        | using usTotal as the parm, via      |
  129.                        | the PowerHABCall. This function call|
  130.                        | will not return until PocessTest    |
  131.                        | is finished.                        |
  132.                        |____________________________________*/
  133.                        PowerHABCall(hAB, ProcessTest, (PVOID)&usTotal);
  134.  
  135.                        WinMessageBox(HWND_DESKTOP,
  136.                                      hWndDlg,
  137.                                      "The thread has finished processing.",
  138.                                      "Thread - Type 1",
  139.                                      0,
  140.                                      MB_OK | MB_ICONEXCLAMATION | MB_MOVEABLE);
  141.  
  142.                        return (MPARAM)(TRUE);
  143.                        break;
  144.  
  145.                   case PB_THREAD_2:
  146.                        WinQueryDlgItemShort(hWndDlg, EF_NUMBER, &usTotal, FALSE);
  147.  
  148.                        /*____________________________________
  149.                        |                                     |
  150.                        | Call the ProcessTest function,      |
  151.                        | using usTotal as the parm, via      |
  152.                        | the PowerCall. This function call   |
  153.                        | will not return until PocessTest    |
  154.                        | is finished.                        |
  155.                        |____________________________________*/
  156.                        PowerCall(ProcessTest, (PVOID)&usTotal);
  157.  
  158.                        WinMessageBox(HWND_DESKTOP,
  159.                                      hWndDlg,
  160.                                      "The thread has finished processing.",
  161.                                      "Thread - Type 2",
  162.                                      0,
  163.                                      MB_OK | MB_ICONEXCLAMATION | MB_MOVEABLE);
  164.  
  165.                        return (MPARAM)(TRUE);
  166.                        break;
  167.  
  168.                   case PB_NO_THREAD:
  169.                        WinQueryDlgItemShort(hWndDlg, EF_NUMBER, &usTotal, FALSE);
  170.  
  171.                        /*____________________________________
  172.                        |                                     |
  173.                        | Call the ProcessTest function,      |
  174.                        | using usTotal as the parm.  It will |
  175.                        | lock up the desktop, until it       |
  176.                        | returns.                            |
  177.                        |____________________________________*/
  178.                        ProcessTest(&usTotal);
  179.  
  180.                        WinMessageBox(HWND_DESKTOP,
  181.                                      hWndDlg,
  182.                                      "The function call has finished processing.",
  183.                                      "No Thread",
  184.                                      0,
  185.                                      MB_OK | MB_ICONEXCLAMATION | MB_MOVEABLE);
  186.  
  187.                        return (MPARAM)(TRUE);
  188.                        break;
  189.  
  190.                   case DID_CANCEL:
  191.                        WinDismissDlg(hWndDlg, TRUE);
  192.                        break;
  193.                  }
  194.               break;
  195.       }
  196.  
  197. return WinDefDlgProc(hWndDlg,
  198.                         msg,
  199.                         mp1,
  200.                         mp2);
  201. }
  202.  
  203. /*__________________________________________________________________________
  204. |                                                                           |
  205. |    Function: ProcessTest                                                  |
  206. |                                                                           |
  207. | Description: Stub function to demo the differct threaded/no-threading     |
  208. |              calls.  The function you pass to the threading calls should  |
  209. |              be define as:                                                |
  210. |                                                                           |
  211. |               <Ret Value> pascal far <FunctionName>(<Parm List>)          |
  212. |                                                                           |
  213. |              When calling though the PowerThread, you can do just         |
  214. |              about anything you can do when it's non-threaded.  Remember, |
  215. |              if you want to do any WinSendMsg (or any other Win calls,    |
  216. |              excluding WinPostMsg), you'll need to create a message       |
  217. |              queue for this thread.                                       |
  218. |                                                                           |
  219. |      Return: SHORT                                                        |
  220. |                                                                           |
  221. | -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
  222. |                            C H A N G E    L O G                           |
  223. | -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
  224. |                                                                           |
  225. |  PRG.    DATE       DESCRIPTION                                           |
  226. |  ----    ---------- ---------------------------------------------------   |
  227. |  RATAJIK 10-01-1994 Initial Developement                                  |
  228. |__________________________________________________________________________*/
  229. VOID pascal far ProcessTest(USHORT *pusTest)
  230. {
  231.  INT j = *pusTest;
  232.  
  233.  /*____________________________________
  234.  |                                     |
  235.  | Loop through j times, sleeping and  |
  236.  | beeping.  When called directly, this|
  237.  | function will lock PM up.  When     |
  238.  | called through the threads, it      |
  239.  | won't! (How cool!)                  |
  240.  |____________________________________*/
  241.  while(j)
  242.     {
  243.      j--;
  244.      DosSleep(1500L);
  245.      DosBeep(101,1);
  246.     }
  247. }
  248.