Click or drag to resize
CoreTaskExtensionsFinallyTResult Method (TaskTResult, FuncTaskTResult, Task)
Execute a cleanup continuation task when a task completes, regardless of the final Status of the antecedent task. If the cleanup action completes successfully, the status of the antecedent is directly applied to the task returned by this method. Otherwise, the status of the faulted or canceled cleanup operation is directly applied to the task returned by this method.

Namespace: Rackspace.Threading
Assembly: Rackspace.Threading (in Rackspace.Threading.dll) Version: 2.1.0-beta1
Syntax
public static Task<TResult> Finally<TResult>(
	this Task<TResult> task,
	Func<Task<TResult>, Task> cleanupFunction
)

Parameters

task
Type: System.Threading.TasksTaskTResult
The antecedent task.
cleanupFunction
Type: SystemFuncTaskTResult, Task
The continuation function to execute when task completes. The continuation function returns a Task representing the asynchronous cleanup operation.

Type Parameters

TResult
The type of the result produced by the antecedent TaskTResult.

Return Value

Type: TaskTResult
A Task representing the asynchronous operation. When the task completes successfully, the Result property will return the result of the antecedent task.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type TaskTResult. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Exceptions
ExceptionCondition
ArgumentNullException

If task is .

-or-

If cleanupFunction is .

Remarks

This code implements support for the following construct without requiring the use of /.

C#
try
{
    return await task.ConfigureAwait(false);
}
finally
{
    await cleanupFunction(task).ConfigureAwait(false);
}

This method ensures that exception information provided by a faulted or canceled task is not wrapped in an additional AggregateException.

Note Notes to Callers

Since the cleanupFunction is executed synchronously, this method should only be used for lightweight continuation functions. This restriction applies only to cleanupFunction itself, not to the Task returned by it.

Version Information

.NET for Windows Store apps

Supported in: Windows 8

.NET Framework

Supported in: 4.5, 4.0, 3.5

Portable Class Library

Supported in: Portable Class Library (Legacy), Portable Class Library

Threading Library

Supported in: 2.1, 2.0, 1.1, 1.0
See Also