You can use "selective reception" so that your task can be interrupted by an external subscriber (another task or main program). You cannot (easily) interrupt a task at an arbitrary point in its execution; instead, the task itself must determine when it will receive recording calls.
So, you probably want to replace your sequence
Put("1"); Put("2"); Put("3"); ... Put("n");
a loop that calls Put once at each iteration, with a select statement that runs every time.
Here is a demo program that I just threw together. The counter is incremented once per second (approximately). The counter value is displayed if Running is true; otherwise, the loop passes silently. The main program alternatively pauses and resumes every 3 seconds.
The else clause in the select statement causes it to continue execution if no records were called.
with Ada.Text_IO; use Ada.Text_IO; procedure T is task Looper is entry Pause; entry Resume; end Looper; task body Looper is Running: Boolean := True; Count: Integer := 0; begin loop select accept Pause do Running := False; end; or accept Resume do Running := True; end; else null; end select; delay 1.0; if Running then Put_Line("Running, Count = " & Integer'Image(Count)); end if; Count := Count + 1; end loop; end Looper; begin
There may be a more elegant approach than this. It has been a long time since I really used Ada.
source share