home *** CD-ROM | disk | FTP | other *** search
- program cpdemo;
-
- { Concurrent programming demonstration program }
-
- {$R-} { Range checking off }
- {$B-} { Boolean short circuit }
- {$S-} { Stack checking off }
- {$I+} { I/O checking on }
- {$N-} { No numeric coprocessor }
- {$D+} { Debug information on }
- {$T+} { Map on }
- {$M 32768,16384,16384}
-
- Uses
- Dos,
- TPCrt,
- CpExec;
-
- procedure DisplayFile1;
- { Display the contents of a text file. }
- const
- filename = 'cpdemo.pas';
- var
- ch: char;
- i: word;
- s: string;
- f: text;
- begin
- TaskWindow(1, 1, 40, 20, Cyan, White, LightGray, False,
- ' Display ' + filename + ' ');
- assign(f, filename);
- repeat
- for i := 1 to 5 do
- begin
- {$i-} reset(f); {$i+}
- if ioresult = 0
- then
- begin
- while not eof(f) do
- begin
- readln(f, s);
- writeln(s);
- TaskSwitch
- end;
- close(f)
- end
- else writeln('Cannot find ', filename);
- end;
- ClaimInput;
- write('Hit any key to continue ');
- while not KeyPressed do
- TaskSwitch;
- ch := ReadKey;
- ReleaseInput;
- ClrScr;
- delay(1000)
- until False
- end;
-
- procedure DisplayFile2;
- { Display the contents of a text file. }
- const
- filename = 'cpexec.pas';
- var
- save: byte;
- s: string;
- f: text;
- begin
- TaskWindow(41, 1, 80, 10, Magenta, White, Cyan, False,
- ' Display ' + filename + ' ');
- assign(f, filename);
- repeat
- reset(f);
- while not eof(f) do
- begin
- readln(f, s);
- writeln(s);
- TaskSwitch
- end;
- close(f);
- save := TextAttr;
- TextBackground(LightGray);
- TextColor(Red + Blink);
- writeln;
- write('5 second delay - other tasks continue');
- TextAttr := save;
- delay(5000)
- until False
- end;
-
- procedure SimpleArithmetic;
- { Do some simple arithmetic. }
- var
- i, j: word;
- begin
- TaskWindow(41, 11, 80, 20, Red, White, Yellow, False,
- ' Some Simple Arithmetic ');
- i := 1;
- j := 1;
- repeat
- writeln(i, ' * ', j, ' = ', i * j:2);
- Inc(i);
- if odd(i)
- then Inc(j);
- TaskSwitch
- until False
- end;
-
- procedure HarderMath;
- { Do some mathematics that's just a little harder. }
- var
- i: word;
- x: real;
- begin
- TaskWindow(41, 11, 80, 20, Red, White, Yellow, False,
- ' Just a Little Harder ');
- x := 0.0;
- repeat
- writeln(i, ' * exp(', x:4:2, ') = ', i * exp(x):4:2);
- Inc(i);
- if odd(i)
- then x := x + 0.1357;
- TaskSwitch
- until False
- end;
-
- var
- ch: char;
- count: word;
- DFtask1, DFtask2, SAtask, HMtask: word;
- PtrDFtask1, PtrDFtask2, PtrSAtask, PtrHMtask: pointer;
-
- begin { main }
- ClrScr;
-
- TaskInstall(@DisplayFile1, 1000, DFtask1, PtrDFtask1);
- TaskInstall(@DisplayFile2, 1000, DFtask2, PtrDFtask2);
- TaskInstall(@SimpleArithmetic, 1000, SAtask, PtrSAtask);
-
- TaskWindow(1, 21, 80, 25, Green, White, Yellow, True, ' Main Program ');
- writeln('Hit any key to issue command ');
- write('To stop program, enter "H"');
- ch := ' ';
- count := 0;
- repeat
- Inc(count);
- if (count mod 200) = 0
- then
- begin
- TaskRemove(HMtask, PtrHMtask, 1000);
- TaskInstall(@SimpleArithmetic, 1000, SAtask, PtrSAtask)
- end
- else if (count mod 100) = 0
- then
- begin
- TaskRemove(SAtask, PtrSAtask, 1000);
- TaskInstall(@HarderMath, 1000, HMtask, PtrHMtask)
- end;
- ClaimInput;
- if KeyPressed
- then
- begin
- ch := ReadKey;
- write(': ');
- readln(ch);
- ReleaseInput
- end
- else
- begin
- ReleaseInput;
- TaskSwitch
- end
- until UpCase(ch) = 'H'
- end.