2022-10-20 17:29:04 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2023-01-17 08:52:26 +01:00
|
|
|
// Copyright (c) 2010-2023 Daniele Teti and the DMVCFramework Team
|
2022-10-20 17:29:04 +02:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
|
|
|
unit MVCFramework.AsyncTask;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils,
|
|
|
|
System.Threading;
|
|
|
|
|
|
|
|
type
|
2022-11-07 15:37:21 +01:00
|
|
|
TMVCAsyncBackgroundTask<T> = reference to function: T;
|
|
|
|
TMVCAsyncSuccessCallback<T> = reference to procedure(const BackgroundTaskResult: T);
|
|
|
|
TMVCAsyncErrorCallback = reference to procedure(const Expt: Exception);
|
|
|
|
TMVCAsyncDefaultErrorCallback = reference to procedure(const Expt: Exception;
|
2022-10-20 17:29:04 +02:00
|
|
|
const ExptAddress: Pointer);
|
|
|
|
|
2022-11-07 15:37:21 +01:00
|
|
|
MVCAsync = class sealed
|
2022-10-20 17:29:04 +02:00
|
|
|
public
|
|
|
|
class function Run<T>(
|
2022-11-07 15:37:21 +01:00
|
|
|
Task: TMVCAsyncBackgroundTask<T>;
|
|
|
|
Success: TMVCAsyncSuccessCallback<T>;
|
|
|
|
Error: TMVCAsyncErrorCallback = nil): ITask;
|
2022-10-20 17:29:04 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
2022-11-07 15:37:21 +01:00
|
|
|
gDefaultTaskErrorHandler: TMVCAsyncDefaultErrorCallback = nil;
|
2022-10-20 17:29:04 +02:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2023-01-28 23:31:00 +01:00
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
2022-10-20 17:29:04 +02:00
|
|
|
uses
|
2023-01-28 23:31:00 +01:00
|
|
|
System.Classes
|
|
|
|
{$IF Defined(MOBILE)}
|
|
|
|
, FMX.DialogService
|
|
|
|
, System.UITypes
|
|
|
|
, FMX.Dialogs
|
|
|
|
{$ENDIF}
|
|
|
|
;
|
2022-10-20 17:29:04 +02:00
|
|
|
|
|
|
|
{ Async }
|
|
|
|
|
2022-11-07 15:37:21 +01:00
|
|
|
class function MVCAsync.Run<T>(
|
|
|
|
Task: TMVCAsyncBackgroundTask<T>;
|
|
|
|
Success: TMVCAsyncSuccessCallback<T>;
|
|
|
|
Error: TMVCAsyncErrorCallback): ITask;
|
2022-10-20 17:29:04 +02:00
|
|
|
var
|
|
|
|
LRes: T;
|
|
|
|
begin
|
|
|
|
Result := TTask.Run(
|
|
|
|
procedure
|
|
|
|
var
|
|
|
|
Ex: Pointer;
|
|
|
|
ExceptionAddress: Pointer;
|
|
|
|
begin
|
|
|
|
Ex := nil;
|
|
|
|
try
|
|
|
|
LRes := Task();
|
|
|
|
if Assigned(Success) then
|
|
|
|
begin
|
|
|
|
TThread.Queue(nil,
|
|
|
|
procedure
|
|
|
|
begin
|
|
|
|
Success(LRes);
|
|
|
|
end);
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
Ex := AcquireExceptionObject;
|
|
|
|
ExceptionAddress := ExceptAddr;
|
|
|
|
TThread.Queue(nil,
|
|
|
|
procedure
|
|
|
|
var
|
|
|
|
LCurrException: Exception;
|
|
|
|
begin
|
|
|
|
LCurrException := Exception(Ex);
|
|
|
|
try
|
|
|
|
if Assigned(Error) then
|
|
|
|
Error(LCurrException)
|
|
|
|
else
|
2022-11-07 15:37:21 +01:00
|
|
|
gDefaultTaskErrorHandler(LCurrException, ExceptionAddress);
|
2022-10-20 17:29:04 +02:00
|
|
|
finally
|
|
|
|
FreeAndNil(LCurrException);
|
|
|
|
end;
|
|
|
|
end);
|
|
|
|
end;
|
|
|
|
end);
|
|
|
|
end;
|
|
|
|
|
|
|
|
initialization
|
|
|
|
|
2022-11-07 15:37:21 +01:00
|
|
|
gDefaultTaskErrorHandler :=
|
|
|
|
procedure(const E: Exception; const ExceptionAddress: Pointer)
|
2022-10-20 17:29:04 +02:00
|
|
|
begin
|
2023-01-28 23:31:00 +01:00
|
|
|
{$IF Defined(MOBILE)}
|
|
|
|
TDialogService.MessageDialog(Format('[%s] %s', [E.ClassName, E.Message]), TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], TMsgDlgBtn.mbOK, 0, nil);
|
|
|
|
{$ELSE}
|
|
|
|
{TODO -oDanieleT -cGeneral : Should be better to inspect if stderr is available}
|
|
|
|
if not (IsConsole or IsLibrary) then
|
|
|
|
begin
|
|
|
|
ShowException(E, ExceptionAddress);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
WriteLn(E.ClassName, ' ', E.Message);
|
|
|
|
end;
|
|
|
|
{$ENDIF}
|
2022-10-20 17:29:04 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|