🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.windows.ms.programmer
Article #1009

Re: Cooperative Multitasking?

#1009
From: Risto Lankinen
Date: Fri, 04 Jan 1991 09:25
49 lines
2738 bytes
jan@prisma.cv.ruu.nl (Jan Buitenhuis) writes:

>Hi,

>I've run into the following problem: my application has to calculate
>for some minutes. It thereby blocks Windows' (non-preemptive)
>Multitasking capabilities. Is there a general way to
>avoid this problem and to make such a procedure cooperative?

Hi!

Calling Yield() periodically during the calculation should work.  There
is, however, another way what I think adds even more functionality:

If the calculation takes that much time to complete, it would be a nice
feature, too, to let the user cancel the operation should it take too
long.  You can kill two flies in one shot by calling PeekMessage() every
now an then within the calculation loop, as below:

#define  ID_CANCELCALC  0xWHATEVER
MSG tmpMsg;
 . . .
   // Within the 'calculation loop' every once in a while do:
   if( PeekMessage(&tmpMsg,hWnd,WM_COMMAND,WMCOMMAND,PM_NOREMOVE) ) {
      if( tmpMsg.wParam==ID_CANCELCALC ) {
         // Remove {WM_COMMAND,IDCANCELCALC} from the queue with an other
         // call to the PeekMessage(,,,,PM_REMOVE), or mark it processed,
         // or refrain from processing it at all in the usual WM_COMMAND
         // case, then cancel the calculation.
      }
   }
   // Und so weiter...

Now, at the time of PeekMessage() call, the other apps have a chance to run,
and at the same time it can be checked whether the user wants to cancel the
lengthy operation.  There are a few things to take into account: 1) Memory
blocks, which aren't locked, may move depending on what the other apps will
do when they get the control, 2) your application might get a rebound call,
in which case you should keep a flag to tell whether the lengthy operation
is active (and deny restart unless so desired), and 3) the calculation will
take even longer due to the task switch overhead, although the difference
between PeekMessage() and Yield() is somewhat insignificant.

terveisin: Risto Lankinen
-- 
Risto Lankinen / product specialist ***************************************
Nokia Data Systems, Technology Dept *  2                              2   *
THIS SPACE INTENTIONALLY LEFT BLANK * 2 -1 is PRIME!  Now working on 2 +1 *
replies: risto@yj.data.nokia.fi     ***************************************
Technical Details
Message-ID: <904@tuura.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!mit-eddie!bloom-beacon!eru!hagbard!sunic!news.funet.fi!fuug!tuura!risto
References: <jan.662932330@prisma>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!mit-eddie!bloom-beacon!eru!hagbard!sunic!news.funet.fi!fuug!tuura!risto
From: risto@tuura.UUCP (Risto Lankinen)
Newsgroups: comp.windows.ms.programmer
Subject: Re: Cooperative Multitasking?
Keywords: multitasking
Message-ID: <904@tuura.UUCP>
Date: 4 Jan 91 09:25:12 GMT
References: <jan.662932330@prisma>
Distribution: comp
Organization: Nokia Data Systems Oy
Lines: 49