home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / 2QSRC.LZH / 2Q.C next >
C/C++ Source or Header  |  1991-05-07  |  5KB  |  199 lines

  1. #define INCL_WIN
  2. #define INCL_DOS
  3. #include <os2.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "2q.h"
  7.  
  8.  /*
  9.   * 2q.c,  By Peter Fitzsimmons.  No rights reserved.
  10.   * 
  11.   * This sample program demonstrates how easy it is to use more than one
  12.   * message queue in a multithreaded program.
  13.   */
  14.  
  15. static HWND hwndFrame, hwndClient, hwndMLE;
  16. static HMQ hmqWork;            /* worker thread's queue handle */
  17.  
  18. static MRESULT EXPENTRY MyWinProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  19. {
  20.     FILE *fin;
  21.  
  22.     switch (msg) {
  23.     case WM_CREATE:
  24.         /* create a MLE in the client window: */
  25.  
  26.         hwndMLE = WinCreateWindow(hwnd,
  27.         WC_MLE,
  28.         NULL,
  29.         MLS_BORDER | WS_VISIBLE | MLS_VSCROLL | MLS_HSCROLL,
  30.         0, 0, 0, 0,
  31.         hwnd,
  32.         HWND_TOP,
  33.         ID_MLE,
  34.         NULL,
  35.         NULL);
  36.         WinSetFocus(HWND_DESKTOP, hwndMLE);
  37.         break;
  38.     case WM_ERASEBACKGROUND:
  39.         return ((MRESULT) TRUE);
  40.     case WM_SIZE:
  41.         /* when frame is resized,  make sure MLE gets adjusted too */
  42.         WinSetWindowPos(hwndMLE, 0,
  43.         5, 5,
  44.         SHORT1FROMMP(mp2) - 10,
  45.         SHORT2FROMMP(mp2) - 10,
  46.         SWP_SIZE | SWP_MOVE);
  47.         break;
  48.     case WM_COMMAND:
  49.         switch (SHORT1FROMMP(mp1)) {
  50.         case IDM_OPEN:
  51.  
  52.             /*
  53.              * Open a text file,  and tell the other thread to load
  54.              * it by sending a user-defined message.
  55.              */
  56.             fin = fopen("2q.c", "rb");
  57.             if (fin && hmqWork) {
  58.                         WinPostQueueMsg(hmqWork, WMU_LOADFILE, (MPARAM) fin, 0);
  59.             }
  60.             else if (!fin)
  61.             WinMessageBox(HWND_DESKTOP, hwnd,
  62.                 "Error: I was expecting 2Q.C to be in the current directory.",
  63.                 "Sample App", 0, MB_OK | MB_ICONEXCLAMATION);
  64.             break;
  65.         case IDM_CLEAR:
  66.  
  67.             /*
  68.              * Send the MLE a message telling to to delete a bunch
  69.              * of characters.  Note that this message can be sent
  70.              * while the other thread is loading the file into the
  71.              * MLE,  which demonstrates two threads sending
  72.              * messages to the same control at the same time.
  73.              */
  74.             WinSendMsg(hwndMLE, MLM_DELETE, 0, (MPARAM) 0x10000);
  75.             break;
  76.         }
  77.         break;
  78.     case WMU_DONEFILE:        /* this user message sent by other
  79.                      * thread */
  80.         if (MBID_YES == WinMessageBox(HWND_DESKTOP, hwnd,
  81.             "Finished.  Load another file?",
  82.             "Sample App", 0, MB_YESNO | MB_ICONQUESTION)) {
  83.         WinPostMsg(hwnd, WM_COMMAND, MPFROMSHORT(IDM_OPEN), 0);
  84.         }
  85.         break;
  86.     case WM_DESTROY:
  87.  
  88.         /*
  89.          * The WM_QUIT message will cause the other thread to fall out
  90.          * of it's queue loop,  and terminate.  This is not really
  91.          * necessary with THIS program,    since it is about to exit()
  92.          * anyway.
  93.          */
  94.         WinPostQueueMsg(hmqWork, WM_QUIT, 0, 0);
  95.     default:
  96.         return (WinDefWindowProc(hwnd, msg, mp1, mp2));
  97.     }
  98.     return (FALSE);
  99. }
  100.  
  101. static void far pascal _loadds WorkThread(void)
  102. {
  103.     HAB hab;
  104.     QMSG qmsg;
  105.     HWND menu;
  106.     int c;
  107.     FILE *f;
  108.     char buf[2];
  109.  
  110.     hab = WinInitialize(0);
  111.     hmqWork = WinCreateMsgQueue(hab, 0);  /*DEFAULT_QUEUE_SIZE);*/
  112.  
  113.     DosSetPrty(PRTYS_THREAD, PRTYC_IDLETIME, 0, 0);
  114.  
  115.     /*- get menu handle of main window */
  116.     menu = WinWindowFromID(hwndFrame, FID_MENU);
  117.  
  118.     while (WinGetMsg(hab, &qmsg, NULL, 0, 0)) {
  119.     switch (qmsg.msg) {
  120.         case WMU_LOADFILE:
  121.         f = (FILE *) (ULONG) qmsg.mp1;
  122.         /* disable "open" menu */
  123.         winEnableMenuItem(menu, IDM_OPEN, FALSE);
  124.         buf[1] = 0;        /* terminate string */
  125.  
  126.         /*
  127.          * This is an incredibly slow way of loading a file into
  128.          * an MLE.  But that is the whole point of this program.
  129.          * You can take as much time as you wish in a
  130.          * non-window-creating-thread.
  131.          */
  132.         while (EOF != (c = getc(f))) {
  133.             if (c == '\n')
  134.             continue;
  135.             buf[0] = (char) c;
  136.             if (!WinSendMsg(hwndMLE, MLM_INSERT, buf, 0))
  137.             break;
  138.         }
  139.         fclose(f);
  140.         winEnableMenuItem(menu, IDM_OPEN, TRUE);
  141.  
  142.         /* tell master thread we are done */
  143.         WinSendMsg(hwndClient, WMU_DONEFILE, 0, 0);
  144.         break;
  145.         /* case etc:... */
  146.     }
  147.     }
  148.     WinDestroyMsgQueue(hmqWork);
  149.     WinTerminate(hab);
  150.     DosExit(EXIT_THREAD, 0);
  151. }
  152.  
  153. int cdecl main(void)
  154. {
  155.     ULONG flstyle;
  156.     HMQ hmq;
  157.     QMSG qmsg;
  158.     HAB hab;
  159.     char *szClass = "2qclass";
  160.  
  161.     hab = WinInitialize(0);
  162.     hmq = WinCreateMsgQueue(hab, 0);    /* DEFAULT_QUEUE_SIZE); */
  163.  
  164.  
  165.     if (!WinRegisterClass(hab, szClass, MyWinProc, CS_SIZEREDRAW, 0))
  166.     return (1);
  167.  
  168.     flstyle = FCF_TITLEBAR | FCF_SYSMENU | FCF_SIZEBORDER | FCF_MINMAX |
  169.     FCF_TASKLIST | FCF_SHELLPOSITION | FCF_MENU;
  170.  
  171.     hwndMLE = 0;
  172.     hwndFrame = WinCreateStdWindow(HWND_DESKTOP,
  173.         0L,                             /* frame-window style            */
  174.     &flstyle,            /* window style                  */
  175.     szClass,            /* class name                    */
  176.     NULL,                /* window title                  */
  177.     0L,                /* default client style          */
  178.     0,                /* resource in executable file   */
  179.     ID_MAIN,            /* resource id                   */
  180.     &hwndClient);            /* receives client window handle */
  181.  
  182.     if (!hwndFrame)
  183.     return (1);
  184.     else {
  185.     static BYTE stack[4096];
  186.     TID tid;
  187.  
  188.     DosCreateThread((PFNTHREAD) WorkThread, &tid, stack + sizeof(stack));
  189.     }
  190.  
  191.     WinShowWindow(hwndFrame, TRUE);
  192.  
  193.     while (WinGetMsg(hab, &qmsg, NULL, 0, 0))
  194.     WinDispatchMsg(hab, &qmsg);
  195.  
  196.     WinDestroyMsgQueue(hmq);
  197.     WinTerminate(hab);
  198. }
  199.