Thaddy, can I access the "suspended" property for the main program as well? Is there such a property in a console (actually not even a console, but a daemon) application?
Mr.MadGuy, I also use the TCriticaSection a lot (as shown earlier), but in order to use it as a tool to stop my main thread, I need one of the sub-threads to EnterCriticalSection. As my sub-threads are not made to stop the main program, the trick would be to make yet another thread the Enters the Critical Section that stops the main thread, what cannot Enter while it is blocked. It is a bit overkill to make one more thread just to stop the main program but it works:
var
CS : tRtlCriticalSection;
function MyThread({%H-}aParam : pointer) : {$IFDEF CPUARM} LongInt {$ELSE} Int64 {$ENDIF};
begin
writeln('Thread launched'); // This should happen earlier then the main program trying to Enter
InitCriticalSection(CS);
EnterCriticalSection(CS);
writeln('Thread finishes');
EndThread;
result := 0;
end;
begin
writeln('Will start thread'); // First step
BeginThread(@MyThread); // Launch the thread
writeln('Thread started'); // It actually happens earlier than the "Thread launched" message from the thread
Sleep(1000); // Need to wait a bit, to make sure the thread is started
writeln('Will try to Enter'); // This must happen after the thread already blocked the Critical Section
EnterCriticalSection(CS); // It is blocked here
writeln('Main program ran on'); // <--- Never happens
end.
Now my only question, if it is actually less clock cycle intense than just do an infinite loop with e.g. Sleep(10000)?