Closing the MainForm when OTL treads are still working with the IOmniCancellationToken does not terminate the threads. Using the following TaskConfiguration code
private CancelToken: IOmniCancellationToken;
CancelToken := CreateOmniCancellationToken;
FWorker := Parallel.ForEach(0, CalcList.Count-1)
.TaskConfig(Parallel.TaskConfig.OnMessage(Self))
.TaskConfig(Parallel.TaskConfig.CancelWith(CancelToken))
.NumTasks(nMax)
.NoWait
.OnStop(procedure (const task: IOmniTask)
begin
task.Invoke(procedure begin
FWorker := nil;
end);
end);
FWorker
.Execute(
procedure (const value: integer)
begin
CalcUnit.EntrySearch(value);
end);
Form.Close CancelToken.Signal ;
Makes the Form closes and the threads go from 'Parallel.ForEach worker' to 'idle Thread worker' but the Threads do not terminate. and the Program hangs. Why do the threads not terminate? what do I do wrong?
Do you check the CancelToken for IsSignalled in your code? The tasks will not be forced to terminate automatically. Instead you have to check for cancellation in your code and exit the task when it is signaled.