/// <summary> /// Helper method to invoke <see cref="E_ResourcePooling.UseResourceAsync{TResource, T}(AsyncResourcePool{TResource}, Func{TResource, Task{T}}, CancellationToken)"/> with callback which synchronously uses the resource. /// </summary> /// <typeparam name="TResource">The type of resources handled by this pool.</typeparam> /// <param name="pool">This <see cref="AsyncResourcePool{TResource}"/>.</param> /// <param name="user">The callback which synchronously uses resource.</param> /// <param name="token">The optional <see cref="CancellationToken"/> to use during resource acquirement.</param> /// <returns>A task which completes after resource has been returned to the pool.</returns> /// <exception cref="NullReferenceException">If this <see cref="AsyncResourcePool{TResource}"/> is <c>null</c>.</exception> public static async Task UseResourceAsync <TResource>(this AsyncResourcePool <TResource> pool, Action <TResource> user, CancellationToken token) { await pool.UseResourceAsync(resource => { user(resource); return(TaskUtils.CompletedTask); }, token); }
/// <summary> /// Helper method to invoke <see cref="E_ResourcePooling.UseResourceAsync{TResource, T}(AsyncResourcePool{TResource}, Func{TResource, Task{T}}, CancellationToken)"/> with callback which asynchronously returns value of type <typeparamref name="T"/>. /// </summary> /// <typeparam name="TResource">The type of resources handled by this pool.</typeparam> /// <typeparam name="T">The type of return value of asynchronous callback.</typeparam> /// <param name="pool">This <see cref="AsyncResourcePool{TResource}"/>.</param> /// <param name="user">The callback which asynchronously returns some value of type <typeparamref name="T"/>.</param> /// <param name="token">The optional <see cref="CancellationToken"/> to use during asynchronous operations inside <paramref name="user"/> callback.</param> /// <returns>A task which returns the result of <paramref name="user"/> on its completion.</returns> /// <exception cref="NullReferenceException">If this <see cref="AsyncResourcePool{TResource}"/> is <c>null</c>.</exception> public static async Task <T> UseResourceAsync <TResource, T>(this AsyncResourcePool <TResource> pool, Func <TResource, Task <T> > user, CancellationToken token) { var retVal = default(T); await pool.UseResourceAsync(async resource => { retVal = await user(resource); }, token); return(retVal); }