VSoft.Awaitable
v0.5.0
이것은 비동기 기능 호출을위한 간단한 라이브러리입니다. OmnithreadLibrary의 래퍼이며 자체 평행 한 기능을 기반으로합니다.
병렬 .async는 간단한 통화를 취소하는 방법을 제공하지 않으며 취소를 알리지 않으며 결과를 반환 할 수 없습니다.
사용 조항에 vsoft.awitable 포함.
TAsync.Configure<string>(
function ( const cancelToken : ICancellationToken) : string
var
i: Integer;
begin
result := ' Hello ' + value ;
for i := 0 to 2000 do
begin
Sleep( 1 );
// in loops, check the token
if cancelToken.IsCancelled then
exit;
end ;
// where api's can take a handle for cancellation, use the token.handle
WaitForSingleObject(cancelToken.Handle, 5000 );
// any unhandled exceptions here will result in the on exception proc being called (if configured)
// raise Exception.Create('Error Message');
end , token);
)
.OnException(
procedure ( const e : Exception)
begin
Label1.Caption := e.Message;
end )
.OnCancellation(
procedure
begin
// clean up
Label1.Caption := ' Cancelled ' ;
end )
.Await(
procedure ( const value : string)
begin
// use result
Label1.Caption := value ;
end );
기능에서 IAwaitable<TResult> 반환 할 수도 있습니다
function LoadAsyncWithToken ( const token : ICancellationToken; const value : string) : IAwaitable<string>;
begin
// configure our async call and return the IAwaitable<string>
result := TAsync.Configure<string>(
function( const cancelToken : ICancellationToken) : string
begin
// .... do some long running thing
result := ' Hello ' + value ;
end , token);
end ;
// for when there is no result to return
function RunIt ( const token : ICancellationToken; const value : string) : IAwaitable;
begin
// configure our async call and return the IAwaitable<string>
result := TAsync.Configure(
procedure( const cancelToken : ICancellationToken)
begin
// .... do some long running thing
end , token);
end ;
procedure UseIt ;
begin
LoadAsyncWithToken( ' param ' , FTokenSource.Token)
.OnException(
procedure ( const e : Exception)
begin
Label1.Caption := e.Message;
end )
.OnCancellation(
procedure
begin
// clean up
Label1.Caption := ' Cancelled ' ;
end )
.Await(
procedure ( const value : string)
begin
// use result
Label1.Caption := value ;
end );
RunIt( ' param ' , FTokenSource.Token)
.OnException(
procedure ( const e : Exception)
begin
Label1.Caption := e.Message;
end )
.OnCancellation(
procedure
begin
// clean up
Label1.Caption := ' Cancelled ' ;
end )
.Await(
procedure
begin
// use result
Label1.Caption := ' Done ' ;
end );
AWAIT은 실제로 비동기 기능을 호출합니다. 취소 할 필요가없는 경우 취소 토큰을 사용하지 않는 Tasync.configure에 과부하가 발생합니다.