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.

Comments

  1. Isn't this what the cancellation tokens are for?

    ReplyDelete
  2. Asbjørn Heid Are you suggesting to use a feature that does not exist to solve this problem? Very helpful ;)

    ReplyDelete
  3. Stefan Glienke Just use your time machine. You did get the complimentary time machine with the latest update didn't you? :P

    I'm assuming here you don't have control over the tasks? Otherwise you could just roll your own cancellation tokens.

    ReplyDelete

Post a Comment