home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MQSRC.LZH / WORKER.C < prev   
C/C++ Source or Header  |  1991-05-07  |  3KB  |  88 lines

  1. #define INCL_WIN
  2. #define INCL_DOS
  3. #include <os2.h>
  4. #include <process.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "mq.h"
  8.  
  9. void far cdecl WorkThread(void far *arg)
  10. {
  11.     INSTANCE *inst;
  12.     HAB hab;
  13.     QMSG qmsg;
  14.     HWND menu;
  15.     int c;
  16.     FILE *f;
  17.     char buf[101];
  18.  
  19.     inst = (INSTANCE *)(ULONG)arg;  /* cast to ULONG first so we can pass
  20.                                      * a near pointer without the compiler
  21.                                      * complaining.  This is now memory
  22.                                      * model independant.
  23.                                      */
  24.  
  25.     hab = WinInitialize(0);
  26.     inst->hmqWork = WinCreateMsgQueue(hab, 0); /*DEFAULT_QUEUE_SIZE);*/
  27.  
  28.  
  29.     /*- get menu handle of main window */
  30.     menu = WinWindowFromID(inst->hwndFrame, FID_MENU);
  31.  
  32.     DosSemClear(&inst->semReady);
  33.  
  34.     /*
  35.      * Set the priority to the lowest value possible.  Most "CPU monitors"
  36.      * contain a flaw which will show that this thread is using a lot of
  37.      * CPU.  It DOES use a lot of CPU,  but it is only using it IF IT IS
  38.      * AVAILABLE.  Programs like MONITOR.EXE should count the number of
  39.      * times their idle-thread is SCHEDULED,  and do a DosSleep(1L) right
  40.      * away.  Instead,  they "soak" up all of the timeslice allotted to
  41.      * them.   (If you run two copies of MONITOR.EXE as the same time,  it
  42.      * will show 50% cpu utilization!).
  43.      */
  44.     DosSetPrty(PRTYS_THREAD, PRTYC_IDLETIME, 1, 0);  /* optional */
  45.  
  46.     winEnableMenuItem(menu, IDM_HALT,  FALSE);
  47.     while (WinGetMsg(hab, &qmsg, NULL, 0, 0)){
  48.         winEnableMenuItem(menu, IDM_OPEN, FALSE); /* disable menu items */
  49.         winEnableMenuItem(menu, IDM_CLEAR, FALSE);
  50.         winEnableMenuItem(menu, IDM_HALT,  TRUE);
  51.         inst->running = TRUE;
  52.         switch(qmsg.msg){
  53.             case WMU_LOADFILE:
  54.                 f = (FILE *)(ULONG)qmsg.mp1;
  55.                 buf[100] = 0;         /* terminate string */
  56.                 while( fgets(buf, sizeof(buf)-1, f) && inst->running){
  57.                     c = strlen(buf)-1;
  58.                     if( buf[c] == '\n' )
  59.                         buf[c] = 0;
  60.                     if(!WinSendMsg(inst->hwndMLE, MLM_INSERT, buf, 0))
  61.                         break;
  62.                     DosSleep(0L);  /* optional: sleep makes the threads
  63.                                     * run smoother together.
  64.                                     */
  65.                 }
  66.                 fclose(f);
  67.                 /* tell master thread we are done */
  68.                 WinSendMsg(inst->hwndClient, WMU_DONEFILE, 0, 0);
  69.                 break;
  70.  
  71.         case WMU_CLEAR:
  72.                 while(inst->running){
  73.                     if( 10L != (ULONG) WinSendMsg(inst->hwndMLE, MLM_DELETE, MPFROMLONG(0L), MPFROMLONG(10L)))
  74.                         break;
  75.                     DosSleep(1L); /* optional */
  76.                 }
  77.         break;
  78.         }
  79.         winEnableMenuItem(menu, IDM_OPEN, TRUE);
  80.         winEnableMenuItem(menu, IDM_CLEAR, TRUE);
  81.         winEnableMenuItem(menu, IDM_HALT,  FALSE);
  82.     }
  83.     DosEnterCritSec();
  84.     WinPostMsg(inst->hwndFrame, WMU_CLOSE, 0, 0);
  85.     WinDestroyMsgQueue(inst->hmqWork);
  86.     WinTerminate(hab);
  87. }
  88.