Show / Hide Table of Contents

Class TaskHelper

Extension methods for the System.Threading.Tasks.Task and System.Threading.Tasks.Task<TResult> classes.

Inheritance
System.Object
TaskHelper
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Kephas.Threading.Tasks
Assembly: Kephas.Core.dll
Syntax
public static class TaskHelper

Properties

| Improve this Doc View Source

CompletedTask

Gets a task that has already completed successfully.

Declaration
public static Task CompletedTask { get; }
Property Value
Type Description
System.Threading.Tasks.Task

A successfully completed task.

| Improve this Doc View Source

DefaultTimeout

Gets or sets the default timeout when waiting for task completion in simulating synchronous calls. The default value is 30 seconds, but it can be changed to accomodate application needs.

Declaration
public static TimeSpan DefaultTimeout { get; set; }
Property Value
Type Description
System.TimeSpan
| Improve this Doc View Source

DefaultWaitMilliseconds

Gets or sets the default value of milliseconds to wait a task in a completion check cycle when simulating synchronous calls. The default value is 20 milliseconds, but it can be changed to accomodate application needs.

Declaration
public static int DefaultWaitMilliseconds { get; set; }
Property Value
Type Description
System.Int32

Methods

| Improve this Doc View Source

AsAsync(Action, Int32, CancellationToken)

Converts the action into an asynchronous operation.

Declaration
public static Task AsAsync(this Action action, int timeout, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type Name Description
System.Action action

The action to be executed.

System.Int32 timeout

The timeout in milliseconds.

System.Threading.CancellationToken cancellationToken

Optional. a token that allows processing to be cancelled.

Returns
Type Description
System.Threading.Tasks.Task

An asynchronous result.

Remarks

Caution is to be considered because, when the action does not complete in the indicated time or when cancellation is issued, the task running the operation does not complete. In this case, the running task is returned in the Task property of either the TaskCanceledException or TaskTimeoutException.

| Improve this Doc View Source

AsAsync(Action, Nullable<TimeSpan>, CancellationToken)

Converts the action into an asynchronous operation.

Declaration
public static Task AsAsync(this Action action, TimeSpan? timeout, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type Name Description
System.Action action

The action to be executed.

System.Nullable<System.TimeSpan> timeout

The timeout. If null specified, indefinite wait is considered.

System.Threading.CancellationToken cancellationToken

Optional. a token that allows processing to be cancelled.

Returns
Type Description
System.Threading.Tasks.Task

An asynchronous result.

Remarks

Caution is to be considered because, when the action does not complete in the indicated time or when cancellation is issued, the task running the operation does not complete. In this case, the running task is returned in the Task property of either the TaskCanceledException or TaskTimeoutException.

| Improve this Doc View Source

AsAsync(Action, CancellationToken)

Converts the action into an asynchronous operation.

Declaration
public static Task AsAsync(this Action action, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type Name Description
System.Action action

The action to be executed.

System.Threading.CancellationToken cancellationToken

Optional. a token that allows processing to be cancelled.

Returns
Type Description
System.Threading.Tasks.Task

An asynchronous result.

Remarks

Caution is to be considered because, when the action does not complete in the indicated time or when cancellation is issued, the task running the operation does not complete. In this case, the running task is returned in the Task property of either the TaskCanceledException or TaskTimeoutException.

| Improve this Doc View Source

AsAsync<T>(Func<T>, Int32, CancellationToken)

Converts the function into an asynchronous operation.

Declaration
public static Task<T> AsAsync<T>(this Func<T> func, int timeout, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type Name Description
System.Func<T> func

The function to be executed.

System.Int32 timeout

The timeout in milliseconds.

System.Threading.CancellationToken cancellationToken

Optional. a token that allows processing to be cancelled.

Returns
Type Description
System.Threading.Tasks.Task<T>

An asynchronous result.

Type Parameters
Name Description
T

The function return type.

Remarks

Caution is to be considered because, when the function does not complete in the indicated time or when cancellation is issued, the task running the operation does not complete. In this case, the running task is returned in the Task property of either the TaskCanceledException or TaskTimeoutException.

| Improve this Doc View Source

AsAsync<T>(Func<T>, Nullable<TimeSpan>, CancellationToken)

Converts the function into an asynchronous operation.

Declaration
public static Task<T> AsAsync<T>(this Func<T> func, TimeSpan? timeout, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type Name Description
System.Func<T> func

The function to be executed.

System.Nullable<System.TimeSpan> timeout

The timeout. If null specified, indefinite wait is considered.

System.Threading.CancellationToken cancellationToken

Optional. a token that allows processing to be cancelled.

Returns
Type Description
System.Threading.Tasks.Task<T>

An asynchronous result.

Type Parameters
Name Description
T

The function return type.

Remarks

Caution is to be considered because, when the function does not complete in the indicated time or when cancellation is issued, the task running the operation does not complete. In this case, the running task is returned in the Task property of either the TaskCanceledException or TaskTimeoutException.

| Improve this Doc View Source

AsAsync<T>(Func<T>, CancellationToken)

Converts the function into an asynchronous operation.

Declaration
public static Task<T> AsAsync<T>(this Func<T> func, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type Name Description
System.Func<T> func

The function to be executed.

System.Threading.CancellationToken cancellationToken

Optional. a token that allows processing to be cancelled.

Returns
Type Description
System.Threading.Tasks.Task<T>

An asynchronous result.

Type Parameters
Name Description
T

The function return type.

Remarks

Caution is to be considered because, when the function does not complete in the indicated time or when cancellation is issued, the task running the operation does not complete. In this case, the running task is returned in the Task property of either the TaskCanceledException or TaskTimeoutException.

| Improve this Doc View Source

EnsureCompletedSuccessfully(Task)

Ensures that the task completed successfully.

Declaration
public static void EnsureCompletedSuccessfully(this Task task)
Parameters
Type Name Description
System.Threading.Tasks.Task task

The task.

| Improve this Doc View Source

GetResultNonLocking<T>(Task<T>, Nullable<TimeSpan>, Nullable<Int32>, Boolean)

Waits the task avoiding the current thread to be locked.

Declaration
public static T GetResultNonLocking<T>(this Task<T> task, TimeSpan? timeout = default(TimeSpan? ), int? waitMilliseconds = default(int? ), bool throwOnTimeout = true)
Parameters
Type Name Description
System.Threading.Tasks.Task<T> task

The task.

System.Nullable<System.TimeSpan> timeout

The timeout (optional). The default value is DefaultTimeout.

System.Nullable<System.Int32> waitMilliseconds

The milliseconds used to wait until checking again the state of the task (optional). The default value is DefaultWaitMilliseconds.

System.Boolean throwOnTimeout

If set to true an exception is thrown on timeout.

Returns
Type Description
T

The task result.

Type Parameters
Name Description
T

The return type.

| Improve this Doc View Source

PreserveThreadContext(Task)

Gets a task awaiter preserving the current context upon continuation.

Declaration
public static ThreadContextAwaiter PreserveThreadContext(this Task task)
Parameters
Type Name Description
System.Threading.Tasks.Task task

The task.

Returns
Type Description
ThreadContextAwaiter

A ThreadContextAwaiter.

Remarks

The returned awaiter does not continue on the captured context (System.Threading.Tasks.Task.ConfigureAwait(System.Boolean)(false) is called), but it can be configured to preserve some thread properties. This awaiter is useful on the server, where await should not continue on the starting thread (like the UI does), but properties like the current culture should be preserved.

| Improve this Doc View Source

PreserveThreadContext<TResult>(Task<TResult>)

Gets a task awaiter preserving the current context upon continuation.

Declaration
public static ThreadContextAwaiter<TResult> PreserveThreadContext<TResult>(this Task<TResult> task)
Parameters
Type Name Description
System.Threading.Tasks.Task<TResult> task

The task.

Returns
Type Description
ThreadContextAwaiter<TResult>

A ThreadContextAwaiter<TResult>.

Type Parameters
Name Description
TResult

Type of the result.

Remarks

The returned awaiter does not continue on the captured context (System.Threading.Tasks.Task.ConfigureAwait(System.Boolean)(false) is called), but it can be configured to preserve some thread properties. This awaiter is useful on the server, where await should not continue on the starting thread (like the UI does), but properties like the current culture should be preserved.

| Improve this Doc View Source

WaitNonLocking(Task, Nullable<TimeSpan>, Nullable<Int32>, Boolean)

Waits the task avoiding the current thread to be locked.

Declaration
public static bool WaitNonLocking(this Task task, TimeSpan? timeout = default(TimeSpan? ), int? waitMilliseconds = default(int? ), bool throwOnTimeout = true)
Parameters
Type Name Description
System.Threading.Tasks.Task task

The task.

System.Nullable<System.TimeSpan> timeout

The timeout (optional). The default value is DefaultTimeout.

System.Nullable<System.Int32> waitMilliseconds

The milliseconds used to wait until checking again the state of the task (optional). The default value is DefaultWaitMilliseconds.

System.Boolean throwOnTimeout

If set to true an exception is thrown on timeout.

Returns
Type Description
System.Boolean

true if the task completed execution within the allotted time; otherwise, false.

Remarks

It is a bad practice to run synchronously tasks meant to be async "by birth". However, if there is no other chance than waiting for a task to complete synchronously, DO NOT USE task.Wait(), because there are situations when it deadlocks the thread. An option would be to use task.ConfigureAwait(false).Wait(), but all the tasks down the task chain must be exactly the same way configured, which may not not be always the case. An alternative implementation might be the one provided below, but this must be tried if it really works: http://stackoverflow.com/questions/5095183/how-would-i-run-an-async-taskt-method-synchronously. For more information see also http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html and http://blogs.msdn.com/b/pfxteam/archive/2012/04/13/10293638.aspx.

| Improve this Doc View Source

WithTimeout(Task, TimeSpan)

Configures a timeout for the provided task. If the task ends within the indicated time, the original task result is returned, otherwise a TaskTimeoutException occurs.

Declaration
public static Task WithTimeout(this Task task, TimeSpan timeout)
Parameters
Type Name Description
System.Threading.Tasks.Task task

The task.

System.TimeSpan timeout

The timeout.

Returns
Type Description
System.Threading.Tasks.Task

The result of the original task.

Remarks

The TaskTimeoutException contains the original task that timed out.

| Improve this Doc View Source

WithTimeout<T>(Task<T>, TimeSpan)

Configures a timeout for the provided task. If the task ends within the indicated time, the original task result is returned, otherwise a TaskTimeoutException occurs.

Declaration
public static Task<T> WithTimeout<T>(this Task<T> task, TimeSpan timeout)
Parameters
Type Name Description
System.Threading.Tasks.Task<T> task

The task.

System.TimeSpan timeout

The timeout.

Returns
Type Description
System.Threading.Tasks.Task<T>

The result of the original task.

Type Parameters
Name Description
T

The task result type.

Remarks

The TaskTimeoutException contains the original task that timed out.

  • Improve this Doc
  • View Source
Back to top Generated by DocFX