yow joseph albahari asynchronous functions in c#...

33
Asynchrony in C# 5 Deep Dive Joe Albahari Asynchronized swimming Beijing Olympics

Upload: vuongxuyen

Post on 20-Oct-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Asynchrony  in  C#  5  Deep  Dive    

Joe  Albahari  Asynchronized  swimming  Beijing  Olympics  

Page 2: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

JOE  ALBAHARI  www.albahari.com  

Page 3: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Asynchronous  Programming  is  the  theme  of  C#  5  (and  VB11)  

…and  Framework  4.5!  

New  keywords!  

(466  new  asynchronous  “TAP”  methods)  

Page 4: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!
Page 5: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

We  need  concurrency.  

But  how?  

Page 6: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Standard  soluPon  =  threading  

Explicit:  new  Thread()...  Implicit:  BackgroundWorker  

Who  enjoys  debugging  mulHthreaded  code?  

Page 7: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

“We’re  here  to  help!”  

Page 8: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

“Your  call  is  important  to  us.      Please  hold  the  line!”  

Page 9: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

“Please  enter  your  phone  number  and  press  #”  

Page 10: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

“We’ll  call  you  back”  

Callback  

20  minutes  later…  

ConPnuaPon  

Page 11: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Sync  vs.  Async  Synchronous  call   Asynchronous  call  Caller  WAITS  for  method  to  complete  

Method  returns  immediately  to  caller,  and  executes  callback  (conPnuaPon)  when  complete  

“Blocking”   “Non-­‐blocking”  

Page 12: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Speakerphone  Concurrency   True  Asynchronous  Call  Caller  manages  concurrency   Callee  manages  concurrency  

Caller  spins  up  thread   Callee  may  start  a  thread  

–  and  WAITS  on  that  thread  

No  callback/conPnuaPon  required  

Callback/conPnuaPon  usually  required  

Page 13: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Why  async?  •  Beber  thread  safety  

•  Less  plumbing  code  

•  Can  avoid  blocking  threads  (if  I/O  bound)  –  Which  improves  scalability  –  And  keeps  the  thread  pool  clean  

•  Can  abstract  concurrency  

Page 14: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!
Page 15: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Framework  1.0:  APM  (IAsyncResult  pabern)  stream.BeginRead(…….)  stream.EndRead(…….)  FAIL!    Requires  therapist    

Framework  2.0:  EAP  webClient.DownloadStringCompleted  +=  ……  webClient.DownloadStringAsync(……..)  FAIL!    Not  composable  

Framework  4.0:  Tasks  

Page 16: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!
Page 17: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

string  GetWebPage  (string  uri)  {      ...      ...  

}  

void  Test()  {      string  html  =  GetWebPage(“...”);      Console.WriteLine  (html);  }  

Page 18: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

void  GetWebPageAsync  (  string  uri,  Action<string>  continuation)  

{      ...      ...  

}  

void  Test()  {      GetWebPageAsync(“...”,  Console.WriteLine);  

}  

Page 19: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Task<string>  GetWebPageAsync  (string  uri)  {      ...      ...  }  

Page 20: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Task<TResult>  Result  property  ExcepHon  property  ConHnueWith()  method  

Task<TResult>  is  a  value-­‐added  signaling  construct  

Tasks  deprecate  the  APM  and  REAP!  

Page 21: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Task<string>  GetWebPageAsync  (string  uri)  {      ...      ...  

}  

void  Test()  {      GetWebPageAsync(“...”).ContinueWith  (task  =>          Console.WriteLine  (task.Result));  }  

Page 22: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Task<string>  GetWebPageAsync  (string  uri)  {      ...      ...  

}  

Task  Test()  {      return          GetWebPageAsync(“...”).ContinueWith  (task  =>              Console.WriteLine  (task.Result));  }  

Page 23: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

string  GetWebPage  (string  uri)  {      ...      ...  }  

void  Test()  {      for  (int  i  =  0;  i  <  5;  i++)      {          string  html  =  GetWebPage(“...”);          Console.WriteLine  (html);      }  }  

Page 24: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Task<string>  GetWebPageAsync  (string  uri)  {      ...      ...  }  

int  _i  =  0;  void  Test()  {      GetWebPageAsync(“...”).ContinueWith  (task  =>        {          Console.WriteLine  (task.Result);          if  (++_i  <  5)  Test();      });  }  

Page 25: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!
Page 26: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!
Page 27: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

ConPnuaPons  and  imperaPve  code  don’t  mix!  

That’s  why  we  need  C#  language  support      aka    asynchronous  funcHons  

Page 28: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Demos  •  LINQPad  Demo  •  VS  Project  Demo  

Page 29: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Hooray!  

Framework  4.5:  

APM      353  methods  REAP      38  methods  TAP        466  methods    

The  TAP  deprecates  the  APM  and  REAP  

Page 30: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

C#  1   C#  2  • Generics  • Nullable  Types  •  Closures  

C#  3  •  Lambda  Expressions  • Query  comprehensions  •  Extension  methods  

C#  4  • Dynamic  Binding  •  Parallel  Programming  (CLR)  

Page 31: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Parallel  Programming  

Task  Parallel  Library  

PLINQ  

FW  4.0  

Latency  

Async  FuncPons  (C#  5)  

Rx  

vNext  

Concurrency  

ImperaHve   ImperaHve  FuncHonal   FuncHonal  

Page 32: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Thinking  asynchronously  •  Stop  thinking  in  terms  of  threads!  

•  Program  synchronously,  then  await  instead  of  wai:ng  

•  Don’t  block  (await  instead)  

•  Keep  concurrency  fine-­‐grained    &  all  the  way  down!  

Page 33: YOW Joseph Albahari Asynchronous Functions in C# 5gotocon.com/.../slides/YOW-Joseph-Albahari-Asynchronous-Functions.pdf · SpeakerphoneConcurrency True)Asynchronous)Call) Caller!manages!concurrency!

Resources  •  CTP:  download  from  MS  (search  “async  CTP”)  

•  MS  Async  CTP  Forum  (search  “async  CTP  forum”)  

•  LINQPad  InteracHve  Async  Tutorial  (click  Download  more  samples  from  LINQPad)