Presentation is loading. Please wait.

Presentation is loading. Please wait.

03 | Async Programming & Networking Intro

Similar presentations


Presentation on theme: "03 | Async Programming & Networking Intro"— Presentation transcript:

1 03 | Async Programming & Networking Intro
8/7/2019 03 | Async Programming & Networking Intro Sean McCune Big Nerd Ranch © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

2 Course Topics Building Windows Store Apps for iOS Developers Jump Start 01 | Introduction to the Windows Store Platform and the tools 02 | C# for Objective-C developers 03 | Async programming & Networking intro Meal Break, around noon PST 45 to 60 mins 04 | Introduction to XAML & UI Patterns for XAML apps 05 | App Model & Storage 06 | Contracts 07 | Notifications 08 | Windows Store APIs

3 WinRT Is Asynchronous Operations > 50 ms are asynchronous
8/7/2019 WinRT Is Asynchronous Operations > 50 ms are asynchronous C# has async/await keywords Can only update UI from main thread © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

4 Cocoa Got Blocks Blocks are lambdas – anonymous functions
8/7/2019 Cocoa Got Blocks Blocks are lambdas – anonymous functions GCD has queues – runs code asynchronously Update UI via block on main queue © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

5 Async HTTP Request With Completion
Build 2012 8/7/2019 Async HTTP Request With Completion void DownloadFileUsingCompletion() { HttpClient client = new HttpClient(); client.GetAsync(" response => { response.Result.Content.ReadAsStringAsync(). ContinueWith( text => { Debug.WriteLine(" "); Debug.WriteLine(text.Result); }); } © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

6 Async HTTP Request With Completion
Build 2012 8/7/2019 Async HTTP Request With Completion void DownloadFileUsingCompletion() { HttpClient client = new HttpClient(); client.GetAsync(" response => { response.Result.Content.ReadAsStringAsync(). ContinueWith( text => { Debug.WriteLine(" "); Debug.WriteLine(text.Result); }); } © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

7 Async HTTP Request With Async/Await
Build 2012 8/7/2019 Async HTTP Request With Async/Await async void DownloadFileUsingAsyncKeyword() { HttpClient client = new HttpClient(); var result = await client.GetAsync(" string text = await result.Content.ReadAsStringAsync(); Debug.WriteLine(" "); Debug.WriteLine(text); SwapButtonTextAsync(); } © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

8 Async HTTP Request With Async/Await
Build 2012 8/7/2019 Async HTTP Request With Async/Await async void DownloadFileUsingAsyncKeyword() { HttpClient client = new HttpClient(); var result = await client.GetAsync(" string text = await result.Content.ReadAsStringAsync(); Debug.WriteLine(" "); Debug.WriteLine(text); SwapButtonTextAsync(); } © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

9 Async HTTP Request With Async/Await
Build 2012 8/7/2019 Async HTTP Request With Async/Await async void DownloadFileUsingAsyncKeyword() { HttpClient client = new HttpClient(); var result = await client.GetAsync(" string text = await result.Content.ReadAsStringAsync(); Debug.WriteLine(" "); Debug.WriteLine(text); SwapButtonTextAsync(); } © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

10 Async HTTP Request With Async/Await
Build 2012 8/7/2019 Async HTTP Request With Async/Await async void DownloadFileUsingAsyncKeyword() { HttpClient client = new HttpClient(); var result = await client.GetAsync(" string text = await result.Content.ReadAsStringAsync(); Debug.WriteLine(" "); Debug.WriteLine(text); SwapButtonTextAsync(); } © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

11 8/7/2019 Demo © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

12 Build 2012 8/7/2019 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

13 Updating The UI From Async Code
Build 2012 8/7/2019 Updating The UI From Async Code async void SwapButtonTextAsync() { await this.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, new DispatchedHandler(() => { if(AsyncButton.Content.ToString() == "Async Keyword") AsyncButton.Content = "Bing!"; else AsyncButton.Content = "Async Keyword"; })); } © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

14 For The Sake of Completeness
Build 2012 8/7/2019 For The Sake of Completeness private void AsyncKeyword_Click(object sender, RoutedEventArgs e) { DownloadFileUsingCompletion(); Debug.WriteLine("Async using completion has returned"); DownloadFileUsingAsyncKeyword(); Debug.WriteLine("Async function has returned"); } © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

15 A Few Notes About Async Methods
8/7/2019 A Few Notes About Async Methods Cannot have ref or out parameters Return types of void, Task or Task<T> Lambdas/anonymous methods can be “async” © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

16 Creating Your Own Async Operation
Build 2012 8/7/2019 Creating Your Own Async Operation async Task<Int64> CalculateFibonacciAsync(Int64 value) { Int64 fib = await Task.Run(() => CalculateFibonacci(value)); Debug.WriteLine("Fibonacci({0}) = {1}", value, fib); return fib; } © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

17 Tasks var taskA = new Task(() => Console.WriteLine(
8/7/2019 Tasks var taskA = new Task(() => Console.WriteLine( "Hello from taskA.")); taskA.Start(); Console.WriteLine("Hello from the calling thread."); /* Output: * Hello from the calling thread. * Hello from taskA. */ © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

18 Web Services Sean McCune Big Nerd Ranch 8/7/2019
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

19 HttpClient try { HttpClient client = new HttpClient();
8/7/2019 HttpClient try { HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync(" response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); } catch(HttpRequestException e) { // Handle the exception } © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

20 8/7/2019 HttpClient Methods The following methods return Task<HttpResponseMessage> GetAsync( Uri ) DeleteAsync( Uri ) PostAsync( Uri, HttpContent ) PutAsync( Uri, HttpContent ) SendAsync( HttpRequestMessage ) © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

21 Alternative Get Request Methods
8/7/2019 Alternative Get Request Methods Task<byte[]> GetByteArrayAsync( Uri uri ) Task<String> GetStringAsync( Uri uri ) Task<Stream> GetStreamAsync( Uri uri ) © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

22 HttpWebRequest – Slide 1 of 2
8/7/2019 HttpWebRequest – Slide 1 of 2 // url, data and headerData are strings HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; if (headerData != null) { request.Headers["Authorization"] = headerData; } byte[] bytes = Encoding.UTF8.GetBytes(data); request.ContentType = "application/x-www-form-urlencoded"; request.Headers["ContentLength"] = bytes.Length.ToString(); © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

23 HttpWebRequest – Slide 2 of 2
8/7/2019 HttpWebRequest – Slide 2 of 2 Stream dataStream = await request.GetRequestStreamAsync(); dataStream.Write(bytes, 0, bytes.Length); await dataStream.FlushAsync(); HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); StreamReader responseDataStream = new StreamReader(response.GetResponseStream()); string postResponse = await responseDataStream.ReadToEndAsync(); © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

24 Web Services Sean McCune Big Nerd Ranch 8/7/2019
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.


Download ppt "03 | Async Programming & Networking Intro"

Similar presentations


Ads by Google