Added "Always" callback in the MVCAsync

This commit is contained in:
Daniele Teti 2024-07-28 23:25:12 +02:00
parent b422dc610e
commit 2e05917df3

View File

@ -34,6 +34,7 @@ type
TMVCAsyncBackgroundTask<T> = reference to function: T;
TMVCAsyncSuccessCallback<T> = reference to procedure(const BackgroundTaskResult: T);
TMVCAsyncErrorCallback = reference to procedure(const Expt: Exception);
TMVCAsyncAlwaysCallback = reference to procedure;
TMVCAsyncDefaultErrorCallback = reference to procedure(const Expt: Exception;
const ExptAddress: Pointer);
@ -42,7 +43,8 @@ type
class function Run<T>(
Task: TMVCAsyncBackgroundTask<T>;
Success: TMVCAsyncSuccessCallback<T>;
Error: TMVCAsyncErrorCallback = nil): ITask;
Error: TMVCAsyncErrorCallback = nil;
Always: TMVCAsyncAlwaysCallback = nil): ITask;
end;
var
@ -66,7 +68,8 @@ uses
class function MVCAsync.Run<T>(
Task: TMVCAsyncBackgroundTask<T>;
Success: TMVCAsyncSuccessCallback<T>;
Error: TMVCAsyncErrorCallback): ITask;
Error: TMVCAsyncErrorCallback;
Always: TMVCAsyncAlwaysCallback): ITask;
var
LRes: T;
begin
@ -98,14 +101,26 @@ begin
LCurrException := Exception(Ex);
try
if Assigned(Error) then
Error(LCurrException)
begin
Error(LCurrException);
end
else
begin
gDefaultTaskErrorHandler(LCurrException, ExceptionAddress);
end;
finally
FreeAndNil(LCurrException);
end;
end);
end;
if Assigned(Always) then
begin
TThread.Queue(nil,
procedure
begin
Always();
end);
end;
end);
end;