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