Consider the following:
uses System.Threading;
procedure TForm1.Button1Click(Sender: TObject);
var
task: ITask;
begin
task := TTask.Run(
procedure()
begin
raise EProgrammerNotFound.Create('test');
end
);
try
task.Wait(); // << raises EAggregateException
except
ShowMessage('ex');
end;
task.Wait(); // no exception. returns true
// task.Status is now TTaskStatus.Exception
end;
I find this irritating. Why does waiting for a task raise an exception one time, but not the other?
Apart from the fact that the Embarcadero documentation on ITask.Wait(..) does not even mention an exception should be expected, I guess they (once again) modelled it after .NET's Task.Wait(..) method which clearly states an exception is to be expected if the task was cancelled or failed with an exception.
Some fine gentleman even reported this to Embarcadero almost five years back:
https://quality.embarcadero.com/browse/RSP-9548
But two years later, it was closed as "Works as expected".
I still don't understand. Can someone elaborate? I believe the implementation is wrong.