csharp 5 async

40
www.dotnet.lv

Upload: valdis-iljuconoks

Post on 15-May-2015

498 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: CSharp 5 Async

www.dotnet.lv

Page 2: CSharp 5 Async

Valdis IljuconoksTechnical Fellow, Software ArchitectMicrosoft MVP

Geta AS, Viiar [email protected]://dotnet.lv/blogs/vi@tech_fellow

Page 3: CSharp 5 Async

C#

Page 4: CSharp 5 Async

5.0

Page 5: CSharp 5 Async

Async

Page 6: CSharp 5 Async

concurrency&

asynchrony

Page 7: CSharp 5 Async

sequentialconcurrent

parallelasynchronous

Page 8: CSharp 5 Async

agenda

predecessorsawaiting tasks

Page 9: CSharp 5 Async

predecessors

Page 10: CSharp 5 Async

Asynchronous Programming Model (APM)

Event-based Asynchronous Pattern (EAP)

Page 11: CSharp 5 Async

C# 1.0Component on a Managed Runtime

C# 2.0Generics

C# 3.0Language Integrated Query

C# 4.0Dynamics

C# 5.0Async

Page 12: CSharp 5 Async

awaiting tasks

Page 13: CSharp 5 Async

‘Task’ is representation of ongoing work

Page 14: CSharp 5 Async

var data = DownloadData(...);ProcessData(data);

var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

DownloadDataAsync ProcessData

STOP

ProcessDataDownloadData

Page 15: CSharp 5 Async

async / await

Page 16: CSharp 5 Async

similar to synchronous code

Page 17: CSharp 5 Async

referred as async method

Page 18: CSharp 5 Async

modifier (async) applied to

• a method• a lambda expression• an anonymous method

Page 19: CSharp 5 Async

keyword (await) used in

• body of an immediately enclosing method

• lambda expression• anonymous method

Page 20: CSharp 5 Async

await NOT used in

• synchronous function• query expression• catch or finally block• in lock statement• unsafe context

Page 21: CSharp 5 Async

absence of an operatorcause a compiler warning

Page 22: CSharp 5 Async

characteristics

Page 23: CSharp 5 Async

Async methods are intended to be non-blocking operations

Page 24: CSharp 5 Async

await expression does not block the current thread while the awaited task

is running

Page 25: CSharp 5 Async

expression signs up the rest of the method as a continuation

Page 26: CSharp 5 Async

and

Page 27: CSharp 5 Async

returns control to the caller of the async method.

Page 28: CSharp 5 Async

Method executes synchronouslytill first ‘await’.

Page 29: CSharp 5 Async

Async methods don't require multithreading

Page 30: CSharp 5 Async

async method doesn't run on its own thread

Page 31: CSharp 5 Async

method runs on the current synchronization context.

Page 32: CSharp 5 Async

Task based async pattern

Page 33: CSharp 5 Async

Async method return types

• void• Task• Task<TResult>

Page 34: CSharp 5 Async

async void FireAndForget(){ await t;}

FireAndForget();

Page 35: CSharp 5 Async

async Task JustSignalCompletionAsync(){ return;}

await JustSignalCompletionAsync();

Page 36: CSharp 5 Async

async Task<int> GetResultsAsync(){ return 5;}

var r = await GetResultsAsync();

Page 37: CSharp 5 Async

Naming conventions

• By convention, the suffix "Async" is added

• Async methods that return void are discouraged

• Exceptions to the naming convention (e.g. event handlers)

Page 38: CSharp 5 Async

?

Page 39: CSharp 5 Async

Valdis IljuconoksTechnical Fellow, Software ArchitectMicrosoft MVP

Geta AS, Viiar [email protected]://dotnet.lv/blogs/vi@tech_fellow

Page 40: CSharp 5 Async

www.dotnet.lv