home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
MQSRC.LZH
/
WORKER.C
< prev
Wrap
C/C++ Source or Header
|
1991-05-07
|
3KB
|
88 lines
#define INCL_WIN
#define INCL_DOS
#include <os2.h>
#include <process.h>
#include <stdio.h>
#include <string.h>
#include "mq.h"
void far cdecl WorkThread(void far *arg)
{
INSTANCE *inst;
HAB hab;
QMSG qmsg;
HWND menu;
int c;
FILE *f;
char buf[101];
inst = (INSTANCE *)(ULONG)arg; /* cast to ULONG first so we can pass
* a near pointer without the compiler
* complaining. This is now memory
* model independant.
*/
hab = WinInitialize(0);
inst->hmqWork = WinCreateMsgQueue(hab, 0); /*DEFAULT_QUEUE_SIZE);*/
/*- get menu handle of main window */
menu = WinWindowFromID(inst->hwndFrame, FID_MENU);
DosSemClear(&inst->semReady);
/*
* Set the priority to the lowest value possible. Most "CPU monitors"
* contain a flaw which will show that this thread is using a lot of
* CPU. It DOES use a lot of CPU, but it is only using it IF IT IS
* AVAILABLE. Programs like MONITOR.EXE should count the number of
* times their idle-thread is SCHEDULED, and do a DosSleep(1L) right
* away. Instead, they "soak" up all of the timeslice allotted to
* them. (If you run two copies of MONITOR.EXE as the same time, it
* will show 50% cpu utilization!).
*/
DosSetPrty(PRTYS_THREAD, PRTYC_IDLETIME, 1, 0); /* optional */
winEnableMenuItem(menu, IDM_HALT, FALSE);
while (WinGetMsg(hab, &qmsg, NULL, 0, 0)){
winEnableMenuItem(menu, IDM_OPEN, FALSE); /* disable menu items */
winEnableMenuItem(menu, IDM_CLEAR, FALSE);
winEnableMenuItem(menu, IDM_HALT, TRUE);
inst->running = TRUE;
switch(qmsg.msg){
case WMU_LOADFILE:
f = (FILE *)(ULONG)qmsg.mp1;
buf[100] = 0; /* terminate string */
while( fgets(buf, sizeof(buf)-1, f) && inst->running){
c = strlen(buf)-1;
if( buf[c] == '\n' )
buf[c] = 0;
if(!WinSendMsg(inst->hwndMLE, MLM_INSERT, buf, 0))
break;
DosSleep(0L); /* optional: sleep makes the threads
* run smoother together.
*/
}
fclose(f);
/* tell master thread we are done */
WinSendMsg(inst->hwndClient, WMU_DONEFILE, 0, 0);
break;
case WMU_CLEAR:
while(inst->running){
if( 10L != (ULONG) WinSendMsg(inst->hwndMLE, MLM_DELETE, MPFROMLONG(0L), MPFROMLONG(10L)))
break;
DosSleep(1L); /* optional */
}
break;
}
winEnableMenuItem(menu, IDM_OPEN, TRUE);
winEnableMenuItem(menu, IDM_CLEAR, TRUE);
winEnableMenuItem(menu, IDM_HALT, FALSE);
}
DosEnterCritSec();
WinPostMsg(inst->hwndFrame, WMU_CLOSE, 0, 0);
WinDestroyMsgQueue(inst->hmqWork);
WinTerminate(hab);
}