So, I have this code (of course simplistic version of some real world code).
So, I have this code (of course simplistic version of some real world code).
It does never finish because when the threadpool that powers the tasks is getting destroyed (System.Threading finalization) it does not cancel the tasks. So how can tasks that I don't have any reference to to manually cancel them be stopped when the system is shutting down?
{$APPTYPE CONSOLE}
uses
Threading,
Classes;
procedure Main;
begin
TTask.Run(
procedure
begin
while TTask.CurrentTask.Status <> TTaskStatus.Canceled do // won't do anything, Status is still Running
begin
TThread.Sleep(1000);
Writeln('tick');
end;
end);
end;
begin
Main;
end.
Edit:
Found one solution to make a protected access hack for TThread and check for IsTerminated but this hardly can be the proper solution.
It does never finish because when the threadpool that powers the tasks is getting destroyed (System.Threading finalization) it does not cancel the tasks. So how can tasks that I don't have any reference to to manually cancel them be stopped when the system is shutting down?
{$APPTYPE CONSOLE}
uses
Threading,
Classes;
procedure Main;
begin
TTask.Run(
procedure
begin
while TTask.CurrentTask.Status <> TTaskStatus.Canceled do // won't do anything, Status is still Running
begin
TThread.Sleep(1000);
Writeln('tick');
end;
end);
end;
begin
Main;
end.
Edit:
Found one solution to make a protected access hack for TThread and check for IsTerminated but this hardly can be the proper solution.
/sub
ReplyDeleteIsn't this what the cancellation tokens are for?
ReplyDeleteAs in feature request RSP-13286
ReplyDeletequality.embarcadero.com - Log in - Embarcadero Technologies
/sub
ReplyDeleteAsbjørn Heid Are you suggesting to use a feature that does not exist to solve this problem? Very helpful ;)
ReplyDeleteStefan Glienke Just use your time machine. You did get the complimentary time machine with the latest update didn't you? :P
ReplyDeleteI'm assuming here you don't have control over the tasks? Otherwise you could just roll your own cancellation tokens.