'===========================================================================
' Subject: MULTITASKER LIKE DEMONSTRATION     Date: 07-15-99 (18:15)       
'  Author: Paul-V Khuong                      Code: QB, QBasic, PDS        
'  Origin: paul_virak_khuong@yahoo.com      Packet: ALGOR.ABC
'===========================================================================
'William wanted me to explain this a bit, so here's the explanation:
'First of, it's a timeslice manager
'The way it works is that there is a global array of all the processes
'defining the sub to be executed, the number of bits of sub to be 
'executed and the line @ which the sub is...

'so, before using it, you have to do functions(a bit like objects) that
'take the line it has to start executing to, the number of bits of code
'it must execute and its handle(its process number)

'There are also 4 other global arrays:arg$, which is to put arguments
'to a function... any function may decide which spots it uses to check
'for arguments, but i think it would be a good idea to use its handle
'as the index
'res$:for storing the results of the function(if any)... the handle 
'as index should apply.
'finished:change to 1 when you're finished... same thing for the index
'var$:so store variable you might want to exchange

'if you use control flow, be careful... You better manipulate the lin
'of the index corresponding to the sub...
'You see, for example, if you goto somewhere and that after the goto, 
'it returns to the start of the function(the line manager??? how do 
'you call that??), the flow will go back to where [the goto in
question]+1

'If it's hard to understand, do step by step(F8?? or is it F9??) it may
'get suddenly clear...
'If not, just email me @ pkhuong@technologist.com...

'Paul-Virak KHUONG

DECLARE SUB manager ()
DECLARE FUNCTION p1% (lin AS INTEGER, time AS INTEGER, handle AS INTEGER)
DECLARE FUNCTION p2% (lin AS INTEGER, time AS INTEGER, handle AS INTEGER)
DEFINT A-Z
TYPE process
 s AS INTEGER
 lin AS INTEGER
 time AS INTEGER
 END TYPE

DIM SHARED process(10) AS process
DIM SHARED args$(10)
DIM SHARED res$(10)
DIM SHARED finished(10) AS INTEGER
DIM SHARED vars$(10)
process(1).s = 1
process(1).time = 1
process(1).lin = 1
process(2).s = 2
process(2).time = 2
process(2).lin = 1
CLS
CALL manager

SUB manager
 DO
  SELECT CASE process(proc).s
   CASE IS = 1
    process(proc).lin = p1(process(proc).lin, process(proc).time, proc)
    GOTO e
   CASE IS = 2
    process(proc).lin = p2(process(proc).lin, process(proc).time, proc)
    GOTO e
   CASE ELSE
    proc = 0
e:   END SELECT
  proc = proc + 1
  LOOP
END SUB

FUNCTION p1 (lin AS INTEGER, time AS INTEGER, handle AS INTEGER)
STATIC var1
lin.dec = lin
lin.exe = lin
 DO
  SELECT CASE lin.exe
   CASE IS = 1
    GOTO p1.1
'   CASE IS = 2
'    GOTO p1.2
'   CASE IS = 3
'    GOTO p1.3
   CASE ELSE
    lin.exe = 1: GOTO p1.1
   END SELECT
p1.n:  lin.dec = lin.dec + 1
  lin.exe = lin.exe + 1
  LOOP WHILE lin.dec < lin + time
  p1 = lin.exe
'  PRINT l; process(1).lin
  EXIT FUNCTION
p1.1: IF finished(2) = 1 THEN PRINT res$(2)
END FUNCTION

FUNCTION p2 (lin AS INTEGER, time AS INTEGER, handle AS INTEGER)
STATIC var1
STATIC time.executed
time.executed = time.executed + 1
lin.dec = lin
lin.exe = lin
 DO
  SELECT CASE lin.exe
   CASE IS = 1
    GOTO p2.1
   CASE IS = 2
    GOTO p2.2
'   CASE IS = 3
'    GOTO p2.3
   CASE ELSE
    lin.exe = 1: GOTO p2.1
   END SELECT
p2.n:  lin.dec = lin.dec + 1
  lin.exe = lin.exe + 1
 
  LOOP WHILE lin.dec < lin + time
  p2 = lin.exe
'  PRINT l; process(1).lin
  EXIT FUNCTION
p2.1: finished(2) = 0: GOTO p2.n
p2.2: res$(2) = STR$(VAL(res$(2)) + 2): finished(2) = 1: GOTO p2.n
END FUNCTION
