SlideShare a Scribd company logo
1 of 15
Asynchronous in DotNet4.5
[object Object],[object Object]
Task & Task<T> ,[object Object],[object Object],http://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=VS.110).aspx
Action<object> action = (object obj) => { Console.WriteLine(&quot;Task={0}, obj={1}, Thread={2}&quot;, Task.CurrentId, obj.ToString(), Thread.CurrentThread.ManagedThreadId); }; // Construct an unstarted task Task t1 = new Task(action, &quot;alpha&quot;); // Cosntruct a started task Task t2 = Task.Factory.StartNew(action, &quot;beta&quot;); // Block the main thread to demonstate that t2 is executing t2.Wait(); // Launch t1  t1.Start(); Console.WriteLine(&quot;t1 has been launched. (Main Thread={0})&quot;, Thread.CurrentThread.ManagedThreadId); // Wait for the task to finish. You may optionally provide a timeout interval or a cancellation token to mitigate situations when the task takes too long to finish. t1.Wait(); // Construct an unstarted task Task t3 = new Task(action, &quot;gamma&quot;); // Run it synchronously t3.RunSynchronously(); // Although the task was run synchrounously, it is a good practice to wait for it which observes for  // exceptions potentially thrown by that task. t3.Wait();
async await ,[object Object],[object Object],[object Object],[object Object],http://msdn.microsoft.com/en-us/library/hh156513(v=VS.110).aspx
public  async   Task<int>  ExampleMethodAsync() { // . . . // At the await expression, execution in this method is suspended and, // if AwaitedProcessAsync has not already finished, control returns // to the caller of ExampleMethodAsync. int exampleInt =  await  AwaitedProcessAsync(); // . . . // The return statement completes the task. Any method that is  // awaiting ExampleMethodAsync can now get the integer result. return exampleInt; }
// An event handler must return void. private  async  void button1_Click(object sender, RoutedEventArgs e) { textBox1.Clear(); // SumPageSizesAsync returns a Task. await SumPageSizesAsync(); textBox1.Text += &quot;Control returned to button1_Click.&quot;; } // The following async lambda expression creates an equivalent anonymous // event handler. button1.Click +=  async  (sender, e) => { textBox1.Clear(); // SumPageSizesAsync returns a Task. await  SumPageSizesAsync(); textBox1.Text += &quot;Control returned to button1_Click.&quot;; }
// The following async method returns a Task<T>. private  async  Task<byte[]> GetByteArrayAsync(Uri currentURI) { // Declare an HttpClient object.  HttpClient client = new HttpClient(); // The GetAsync method returns a Task(Of T), where T is an HttpResponseMessage. Task<HttpResponseMessage> httpRMTask = client.GetAsync(currentURI); // Await httpRMTask evaluates to an HttpResponseMessage object. HttpResponseMessage httpRM =  await  httpRMTask; // The following line can replace the previous two assignment statements . //HttpResponseMessage httpRM =  await  client.GetAsync(currentURI); // Throw an exception if the HttpResponseMessage contains an error code. httpRM.EnsureSuccessStatusCode(); // Use ReadAsByteArray to access the content of the resource as a byte array. return httpRM.Content.ReadAsByteArray(); }
Task Parallelism http://msdn.microsoft.com/en-us/library/dd537609(v=VS.110).aspx   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],http://msdn.microsoft.com/en-us/library/hh191443(v=VS.110).aspx
Async Samples ,[object Object]
[object Object]
public void AsyncIntroSerialBefore() { var client = new WebClient(); client.DownloadStringCompleted += AsyncIntroSerialBefore_DownloadStringCompleted_1; client.DownloadStringAsync(new Uri(&quot;http://www.weather.gov&quot;)); } void AsyncIntroSerialBefore_DownloadStringCompleted_1(object sender, DownloadStringCompletedEventArgs e) { WriteLinePageTitle(e.Result); var client = new WebClient(); client.DownloadStringCompleted += AsyncIntroSerialBefore_DownloadStringCompleted_2; client.DownloadStringAsync(new Uri(&quot;http://www.weather.gov/climate/&quot;)); } void AsyncIntroSerialBefore_DownloadStringCompleted_2(object sender, DownloadStringCompletedEventArgs e) { WriteLinePageTitle(e.Result); var client = new WebClient(); client.DownloadStringCompleted += AsyncIntroSerialBefore_DownloadStringCompleted_3; client.DownloadStringAsync(new Uri(&quot;http://www.weather.gov/rss/&quot;)); } void AsyncIntroSerialBefore_DownloadStringCompleted_3(object sender, DownloadStringCompletedEventArgs e) { WriteLinePageTitle(e.Result); } C# 4.0
public async void AsyncIntroSerial() { var client = new WebClient(); WriteLinePageTitle(await client.DownloadStringTaskAsync(new Uri(&quot;http://www.weather.gov&quot;))); WriteLinePageTitle(await client.DownloadStringTaskAsync(new Uri(&quot;http://www.weather.gov/climate/&quot;))); WriteLinePageTitle(await client.DownloadStringTaskAsync(new Uri(&quot;http://www.weather.gov/rss/&quot;))); } Async
Resources http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-829T

More Related Content

What's hot

Debugging over tcp and http
Debugging over tcp and httpDebugging over tcp and http
Debugging over tcp and http
Kaniska Mandal
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
aeden_brines
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
ChaAstillas
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
gerrell
 

What's hot (19)

OTP application (with gen server child) - simple example
OTP application (with gen server child) - simple exampleOTP application (with gen server child) - simple example
OTP application (with gen server child) - simple example
 
Debugging over tcp and http
Debugging over tcp and httpDebugging over tcp and http
Debugging over tcp and http
 
Tdd iPhone For Dummies
Tdd iPhone For DummiesTdd iPhone For Dummies
Tdd iPhone For Dummies
 
Testing logging in asp dot net core
Testing logging in asp dot net coreTesting logging in asp dot net core
Testing logging in asp dot net core
 
692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w
 
Async await
Async awaitAsync await
Async await
 
Closures
ClosuresClosures
Closures
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
Lecture5
Lecture5Lecture5
Lecture5
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
 
Firefox Easily Analyzed by PVS-Studio Standalone
Firefox Easily Analyzed by PVS-Studio StandaloneFirefox Easily Analyzed by PVS-Studio Standalone
Firefox Easily Analyzed by PVS-Studio Standalone
 
PVS-Studio vs Chromium. 3-rd Check
PVS-Studio vs Chromium. 3-rd CheckPVS-Studio vs Chromium. 3-rd Check
PVS-Studio vs Chromium. 3-rd Check
 
Call Back
Call BackCall Back
Call Back
 
Call Back
Call BackCall Back
Call Back
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
Angular 4 The new Http Client Module
Angular 4 The new Http Client ModuleAngular 4 The new Http Client Module
Angular 4 The new Http Client Module
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping new
 

Viewers also liked (12)

Blogs in the Classroom
Blogs in the ClassroomBlogs in the Classroom
Blogs in the Classroom
 
Python with dot net and vs2010
Python with dot net and vs2010Python with dot net and vs2010
Python with dot net and vs2010
 
Sensei kukikan
Sensei kukikanSensei kukikan
Sensei kukikan
 
Connect Composition Student Registration Information
Connect Composition Student Registration InformationConnect Composition Student Registration Information
Connect Composition Student Registration Information
 
Registering for Connect Writing (ENG091)
Registering for Connect Writing (ENG091)Registering for Connect Writing (ENG091)
Registering for Connect Writing (ENG091)
 
Visual studio 11 developer preview
Visual studio 11 developer previewVisual studio 11 developer preview
Visual studio 11 developer preview
 
Using google appengine
Using google appengineUsing google appengine
Using google appengine
 
Using google appengine_final2
Using google appengine_final2Using google appengine_final2
Using google appengine_final2
 
Using google appengine_1027
Using google appengine_1027Using google appengine_1027
Using google appengine_1027
 
Unit 1 abg izhar
Unit 1 abg izharUnit 1 abg izhar
Unit 1 abg izhar
 
Promoting the Use of Social Media in Education
Promoting the Use of Social Media in Education Promoting the Use of Social Media in Education
Promoting the Use of Social Media in Education
 
Student Presentation: To Kill a Mockingbird
Student Presentation: To Kill a MockingbirdStudent Presentation: To Kill a Mockingbird
Student Presentation: To Kill a Mockingbird
 

Similar to Asynchronous in dot net4

Windows Phone 8 - 3.5 Async Programming
Windows Phone 8 - 3.5 Async ProgrammingWindows Phone 8 - 3.5 Async Programming
Windows Phone 8 - 3.5 Async Programming
Oliver Scheer
 
Understanding ASP.NET Under The Cover - Miguel A. Castro
Understanding ASP.NET Under The Cover - Miguel A. CastroUnderstanding ASP.NET Under The Cover - Miguel A. Castro
Understanding ASP.NET Under The Cover - Miguel A. Castro
Mohammad Tayseer
 
Android development training programme , Day 3
Android development training programme , Day 3Android development training programme , Day 3
Android development training programme , Day 3
DHIRAJ PRAVIN
 
Asynchronous reading and writing http r equest
Asynchronous reading and writing http r equestAsynchronous reading and writing http r equest
Asynchronous reading and writing http r equest
Pragyanshis Patnaik
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpoint
webhostingguy
 

Similar to Asynchronous in dot net4 (20)

Sync with async
Sync with  asyncSync with  async
Sync with async
 
Async Programming in C# 5
Async Programming in C# 5Async Programming in C# 5
Async Programming in C# 5
 
Windows Phone 8 - 3.5 Async Programming
Windows Phone 8 - 3.5 Async ProgrammingWindows Phone 8 - 3.5 Async Programming
Windows Phone 8 - 3.5 Async Programming
 
Conociendo el consumo de datos en Windows Phone 8.1
Conociendo el consumo de datos en Windows Phone 8.1Conociendo el consumo de datos en Windows Phone 8.1
Conociendo el consumo de datos en Windows Phone 8.1
 
Ddd melbourne 2011 C# async ctp
Ddd melbourne 2011  C# async ctpDdd melbourne 2011  C# async ctp
Ddd melbourne 2011 C# async ctp
 
Understanding ASP.NET Under The Cover - Miguel A. Castro
Understanding ASP.NET Under The Cover - Miguel A. CastroUnderstanding ASP.NET Under The Cover - Miguel A. Castro
Understanding ASP.NET Under The Cover - Miguel A. Castro
 
Android development training programme , Day 3
Android development training programme , Day 3Android development training programme , Day 3
Android development training programme , Day 3
 
동기화 시대를 뛰어넘는 비동기 프로그래밍
동기화 시대를 뛰어넘는 비동기 프로그래밍동기화 시대를 뛰어넘는 비동기 프로그래밍
동기화 시대를 뛰어넘는 비동기 프로그래밍
 
Session 9 Android Web Services - Part 2.pdf
Session 9 Android Web Services - Part 2.pdfSession 9 Android Web Services - Part 2.pdf
Session 9 Android Web Services - Part 2.pdf
 
Asynchronous reading and writing http r equest
Asynchronous reading and writing http r equestAsynchronous reading and writing http r equest
Asynchronous reading and writing http r equest
 
Concurrency - responsiveness in .NET
Concurrency - responsiveness in .NETConcurrency - responsiveness in .NET
Concurrency - responsiveness in .NET
 
Implementing Comet using PHP
Implementing Comet using PHPImplementing Comet using PHP
Implementing Comet using PHP
 
JavaScript Lessons 2023
JavaScript Lessons 2023JavaScript Lessons 2023
JavaScript Lessons 2023
 
Introducing Struts 2
Introducing Struts 2Introducing Struts 2
Introducing Struts 2
 
Deep Dive async/await in Unity with UniTask(EN)
Deep Dive async/await in Unity with UniTask(EN)Deep Dive async/await in Unity with UniTask(EN)
Deep Dive async/await in Unity with UniTask(EN)
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpoint
 
Training – Going Async
Training – Going AsyncTraining – Going Async
Training – Going Async
 
Bot builder v4 HOL
Bot builder v4 HOLBot builder v4 HOL
Bot builder v4 HOL
 
Finagle and Java Service Framework at Pinterest
Finagle and Java Service Framework at PinterestFinagle and Java Service Framework at Pinterest
Finagle and Java Service Framework at Pinterest
 
2 Asp Dot Net Ajax Extensions
2 Asp Dot Net Ajax Extensions2 Asp Dot Net Ajax Extensions
2 Asp Dot Net Ajax Extensions
 

More from Wei Sun

Using google appengine_final
Using google appengine_finalUsing google appengine_final
Using google appengine_final
Wei Sun
 
Using google appengine (2)
Using google appengine (2)Using google appengine (2)
Using google appengine (2)
Wei Sun
 
Gc algorithm inside_dot_net
Gc algorithm inside_dot_netGc algorithm inside_dot_net
Gc algorithm inside_dot_net
Wei Sun
 
Code review
Code reviewCode review
Code review
Wei Sun
 
Windbg dot net_clr2
Windbg dot net_clr2Windbg dot net_clr2
Windbg dot net_clr2
Wei Sun
 
The best way to learn java script
The best way to learn java scriptThe best way to learn java script
The best way to learn java script
Wei Sun
 
Code quality
Code qualityCode quality
Code quality
Wei Sun
 
老友记
老友记老友记
老友记
Wei Sun
 
Lua gc代码
Lua gc代码Lua gc代码
Lua gc代码
Wei Sun
 
Windbg dot net_clr2
Windbg dot net_clr2Windbg dot net_clr2
Windbg dot net_clr2
Wei Sun
 
Dotnetintroduce 100324201546-phpapp02
Dotnetintroduce 100324201546-phpapp02Dotnetintroduce 100324201546-phpapp02
Dotnetintroduce 100324201546-phpapp02
Wei Sun
 
Web development overview
Web development overviewWeb development overview
Web development overview
Wei Sun
 
DotNet Introduction
DotNet IntroductionDotNet Introduction
DotNet Introduction
Wei Sun
 

More from Wei Sun (16)

Using google appengine_final
Using google appengine_finalUsing google appengine_final
Using google appengine_final
 
Using google appengine (2)
Using google appengine (2)Using google appengine (2)
Using google appengine (2)
 
Gc algorithm inside_dot_net
Gc algorithm inside_dot_netGc algorithm inside_dot_net
Gc algorithm inside_dot_net
 
Code review
Code reviewCode review
Code review
 
Windbg dot net_clr2
Windbg dot net_clr2Windbg dot net_clr2
Windbg dot net_clr2
 
The best way to learn java script
The best way to learn java scriptThe best way to learn java script
The best way to learn java script
 
Code quality
Code qualityCode quality
Code quality
 
老友记
老友记老友记
老友记
 
Lua gc代码
Lua gc代码Lua gc代码
Lua gc代码
 
Windbg dot net_clr2
Windbg dot net_clr2Windbg dot net_clr2
Windbg dot net_clr2
 
Dotnetintroduce 100324201546-phpapp02
Dotnetintroduce 100324201546-phpapp02Dotnetintroduce 100324201546-phpapp02
Dotnetintroduce 100324201546-phpapp02
 
Code rule
Code ruleCode rule
Code rule
 
Web development overview
Web development overviewWeb development overview
Web development overview
 
Lua
LuaLua
Lua
 
DotNet Introduction
DotNet IntroductionDotNet Introduction
DotNet Introduction
 
Mac
MacMac
Mac
 

Recently uploaded

Recently uploaded (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Asynchronous in dot net4

  • 2.
  • 3.
  • 4. Action<object> action = (object obj) => { Console.WriteLine(&quot;Task={0}, obj={1}, Thread={2}&quot;, Task.CurrentId, obj.ToString(), Thread.CurrentThread.ManagedThreadId); }; // Construct an unstarted task Task t1 = new Task(action, &quot;alpha&quot;); // Cosntruct a started task Task t2 = Task.Factory.StartNew(action, &quot;beta&quot;); // Block the main thread to demonstate that t2 is executing t2.Wait(); // Launch t1 t1.Start(); Console.WriteLine(&quot;t1 has been launched. (Main Thread={0})&quot;, Thread.CurrentThread.ManagedThreadId); // Wait for the task to finish. You may optionally provide a timeout interval or a cancellation token to mitigate situations when the task takes too long to finish. t1.Wait(); // Construct an unstarted task Task t3 = new Task(action, &quot;gamma&quot;); // Run it synchronously t3.RunSynchronously(); // Although the task was run synchrounously, it is a good practice to wait for it which observes for // exceptions potentially thrown by that task. t3.Wait();
  • 5.
  • 6. public async Task<int> ExampleMethodAsync() { // . . . // At the await expression, execution in this method is suspended and, // if AwaitedProcessAsync has not already finished, control returns // to the caller of ExampleMethodAsync. int exampleInt = await AwaitedProcessAsync(); // . . . // The return statement completes the task. Any method that is // awaiting ExampleMethodAsync can now get the integer result. return exampleInt; }
  • 7. // An event handler must return void. private async void button1_Click(object sender, RoutedEventArgs e) { textBox1.Clear(); // SumPageSizesAsync returns a Task. await SumPageSizesAsync(); textBox1.Text += &quot;Control returned to button1_Click.&quot;; } // The following async lambda expression creates an equivalent anonymous // event handler. button1.Click += async (sender, e) => { textBox1.Clear(); // SumPageSizesAsync returns a Task. await SumPageSizesAsync(); textBox1.Text += &quot;Control returned to button1_Click.&quot;; }
  • 8. // The following async method returns a Task<T>. private async Task<byte[]> GetByteArrayAsync(Uri currentURI) { // Declare an HttpClient object. HttpClient client = new HttpClient(); // The GetAsync method returns a Task(Of T), where T is an HttpResponseMessage. Task<HttpResponseMessage> httpRMTask = client.GetAsync(currentURI); // Await httpRMTask evaluates to an HttpResponseMessage object. HttpResponseMessage httpRM = await httpRMTask; // The following line can replace the previous two assignment statements . //HttpResponseMessage httpRM = await client.GetAsync(currentURI); // Throw an exception if the HttpResponseMessage contains an error code. httpRM.EnsureSuccessStatusCode(); // Use ReadAsByteArray to access the content of the resource as a byte array. return httpRM.Content.ReadAsByteArray(); }
  • 9.
  • 10.
  • 11.
  • 12.
  • 13. public void AsyncIntroSerialBefore() { var client = new WebClient(); client.DownloadStringCompleted += AsyncIntroSerialBefore_DownloadStringCompleted_1; client.DownloadStringAsync(new Uri(&quot;http://www.weather.gov&quot;)); } void AsyncIntroSerialBefore_DownloadStringCompleted_1(object sender, DownloadStringCompletedEventArgs e) { WriteLinePageTitle(e.Result); var client = new WebClient(); client.DownloadStringCompleted += AsyncIntroSerialBefore_DownloadStringCompleted_2; client.DownloadStringAsync(new Uri(&quot;http://www.weather.gov/climate/&quot;)); } void AsyncIntroSerialBefore_DownloadStringCompleted_2(object sender, DownloadStringCompletedEventArgs e) { WriteLinePageTitle(e.Result); var client = new WebClient(); client.DownloadStringCompleted += AsyncIntroSerialBefore_DownloadStringCompleted_3; client.DownloadStringAsync(new Uri(&quot;http://www.weather.gov/rss/&quot;)); } void AsyncIntroSerialBefore_DownloadStringCompleted_3(object sender, DownloadStringCompletedEventArgs e) { WriteLinePageTitle(e.Result); } C# 4.0
  • 14. public async void AsyncIntroSerial() { var client = new WebClient(); WriteLinePageTitle(await client.DownloadStringTaskAsync(new Uri(&quot;http://www.weather.gov&quot;))); WriteLinePageTitle(await client.DownloadStringTaskAsync(new Uri(&quot;http://www.weather.gov/climate/&quot;))); WriteLinePageTitle(await client.DownloadStringTaskAsync(new Uri(&quot;http://www.weather.gov/rss/&quot;))); } Async