TPL – Async

How to use Task (TPL) in C#

To start with this will just be my notes to learn more about using TPL.


 


 

Fx. I need to call a WebService or WebApi from a Windows Phone App / Store App.

 

http://dotnetcodr.com/2014/01/17/getting-a-return-value-from-a-task-with-c/

.NET 4.5

The recommended way in .NET 4.5 is to use Task.FromResult, Task.Run or Task.Factory.StartNew:

FromResult: [Task.FromResult() is NOT async! It’s just a wrapper to get a Task with Status “RanToCompletion”.
Your calling Thread will block until your operation is completed.]

1
2
3
4
5
6
7
8
9
public async Task DoWork()
{
       int res = await Task.FromResult<int>(GetSum(4, 5));   
}
private int GetSum(int a, int b)
{
    return a + b;
}

 

 

 

 

 

 

 

Tags:

Task, TPL, async

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s