Demonstration of working with threads: creation, suspending, resuming work.
Each script starts at least one thread, which is usually called the “main” thread. In addition to it, you can launch other threads by specifying which procedures should be executed in them.
In this example, in addition to the main thread, 2 more procedures are described, all of them print their unique string into the logs every 500 ms. But in addition to this, a timer is set in the 1st thread, which 1.5 seconds after starting suspends the main and 2nd threads (i.e., all threads except itself). 1st thread continues his work for another 2 seconds, and then finish his work. At the same time, the 2nd and main threads continue to work.
procedure Thread1();
var T: Int64;
begin
T:= GetTickCount();
while Delay(500) do begin
Print('Thread 1');
if (GetTickCount() > T+1500) then begin
Print('Suspend "Main Thread" and "Thread 2"...');
Script.Suspend();
T:= GetTickCount();
while Delay(500) and (GetTickCount() < T+2000) do Print('Thread 1');
Print('Resume all threads...');
Script.Resume();
Exit;
end;
end;
end;
procedure Thread2();
begin
while Delay(500) do Print('Thread 2');
end;
begin
Script.NewThread(@Thread1);
Script.NewThread(@Thread2);
while Delay(500) do Print('Main Thread');
Delay(-1);
end.
Main Thread
Thread 2
Thread 1
Main Thread
Thread 2
Thread 1
Thread 1
Main Thread
Thread 2
Main Thread
Thread 2
Thread 1
Suspend "Main Thread" and "Thread 2"...
Thread 1
Thread 1
Thread 1
Thread 1
Thread 1
Resume all threads...
Main Thread
Thread 2
Main Thread
Thread 2
Main Thread
Thread 2
...