Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C# 5.0
Tip/Trick

New Features in the .NET Framework 4.5.3 Related to Task\TPL

Rate me:
Please Sign up or sign in to vote.
4.94/5 (8 votes)
4 Nov 2014CPOL2 min read 29.6K   6   2
This will focus the new improvements in the .NET Framework 4.5.3 related to Task\Task Parallel Library

Introduction

This tip is for the early birds who want to get their hands dirty with the latest technology and see if they can use it in situations when needed to get the most out of it.

Recently, Visual Studio 14 CTP 4 has been released. It also has new release of .NET Framework 4.5.3

Here are some of the new features\improvements which sound interesting to me.

property “CompletedTask”

What if you want to return a completed task? Here are your options in the previous versions of .NET Framework.

  • TaskCompletionSource (introduced in .NET 4)
  • Task.FromResult (available from .NET 4.5)

And in latest version of .NET Framework (4.5.3)

  • public static Task CompletedTask { get; }

Task Parallel Library already has such object and it’s using it internally quite often. Luckily, you can get access to it now in .NET Framework 4.5.3.

Here is the implementation before .Net Framework (4.5.3)

C#
Public static Task WorkAsync()
{
    return Task.FromResult(0);
}

Below is the alternate way if you decide to use .NET Framework 4.5.3.

C#
Public static Task WorkAsync()
{
    return Task.CompletedTask;
}

This newly available property is implemented in singleton mode and non-thread-safe environment, so you will be using the same completed task most of the time (it may not always return the same instance).

This is beneficial in the sense of not creating new object always and hence putting less work for garbage collector. If you don’t want to compromise on performance in the parallel processing, then it is the most efficient way available so far.

method “FromException”

C#
Public static Task FromException(Exception exception);
Public static Task<TResult> FromException<TResult>(Exception exception);

This newly available method gives you the functionality of creating a Task that has completed in the Faulted state with the specified exception. When you want to generate Task which changed in an error state as well as completion state, then you can use this method.

If you are not using .NET Framework 4.5.3, you can do the same thing like this:

C#
Public static Task WorkAsync( )
{
    var tcs = new TaskCompletionSource<int>( );
    tcs.SetException(new Exception("my exception"));
    return tcs.Task;
}

Below is the alternate way if you are using .NET Framework 4.5.3 which you can see is more efficient, clear and shorter.

C#
Public static Task WorkAsync()
{
    return Task.FromException(new Exception("my exception"));
}

method “FromCanceled”

C#
Public static Task FromCanceled(CancellationToken cancellationToken);
Public static Task<TResult> FromCanceled<TResult>(CancellationToken cancellationToken);

This newly available method gives you the functionality of creating a Task that has completed in the Canceled state with the specified CancellationToken. (I hope the .NET team will change or address the name conflict of this method and internal implementation.)

If you are not using .NET Framework 4.5.3, then you can do the same thing like this:

C#
Public static Task WorkAsync()
{
     var tcs = new TaskCompletionSource<int>();
    tcs.SetCanceled();
    return tcs.Task;
}

Below is the alternate way if you are using .NET Framework 4.5.3 which you can see is more clear and shorter.

C#
Public static Task WorkAsync()
{
    return Task.FromCanceled(new CancellationToken(true));
}

Argument Instance of CancellationToken’s property IsCancellationRequested must be true in order to proceed without any exception, otherwise it will through ArgumentOutOfRangeException exception.

C#
//internal implementation
if(!cancellationToken.IsCancellationRequested) 
throw new ArgumentOutOfRangeException("cancellationToken");

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 5 Pin
Аslam Iqbal4-Nov-14 21:47
professionalАslam Iqbal4-Nov-14 21:47 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun4-Nov-14 18:35
Humayun Kabir Mamun4-Nov-14 18:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.