Asynchronous programming Using Task async and await Asynchronous

  • Slides: 5
Download presentation
Asynchronous programming Using Task, async and await Asynchronous programming 1

Asynchronous programming Using Task, async and await Asynchronous programming 1

Responsive user interfaces • Users expect modern user interfaces to be responsive • If

Responsive user interfaces • Users expect modern user interfaces to be responsive • If the user interface must not “freeze” if the user starts a long-running task. • Example long-running tasks • Sending or receiving data on a network • Communication with a potentially slow server • Including database servers, web servers, etc. • Performing time consuming calculations • Reading or writing files on the local hard disk • When the user initiates a task that is assumed to be long-running we must execute the task in a separate thread • “Separate” means not in the GUI thread Asynchronous programming 2

Keywords: await and async • Two C# keywords: await + async • Syntax: await

Keywords: await and async • Two C# keywords: await + async • Syntax: await expression • Example: Await Task. when. All(task 1, task 2, …. , task. N) • Awaits termination of all task listed • await is used to await(!) the termination of a task • Normally waiting for the result of the task. • In the meantime the method that called the method containing the await will execute • When the awaited expression has terminated, control comes back to the method … • Which can then use the result to display, etc. • A method is declared async if there is an await statement inside the method • Example: Gui. Example. Async. Wait Asynchronous programming 3

API async methods • Some classes has support for async programming • Example: Http.

API async methods • Some classes has support for async programming • Example: Http. Client • With Http. Client you can download the contents of a document specifying the documents URL • Task<string> get. String. Task = http. Client. get. String. Async(“http: //www. easj. dk/”); • String url. Contents = await get. String. Task; • http: //msdn. microsoft. com/en-us/library/hh 191443. aspx • Example: Simple. Browser. Async • System. IO. Stream has some async methods Asynchronous programming 4

References and further readings • MSDN Asynchronous Programming with Async and Await • http:

References and further readings • MSDN Asynchronous Programming with Async and Await • http: //msdn. microsoft. com/en-us/library/hh 191443. aspx • John Sharp: Microsoft Visual C# 2012 Step by Step, • Chapter 24 Improving Response Time by Performing Asynchronous Operations, page 585 -599 • Deitel & Deitel: Visual C# 2013, How To Programing, 5 th edition, Pearson 2014 • Chapter 28 Asynchronous Programming with async and await, 26 pages • Available as a web-only chapter from http: //www. pearsoninternationaleditions. com/Sitemap/Deitel/, Companion Web site • http: //wps. pearsoned. co. uk/wps/media/access/Pearson_Default/15358/15727496/login. ht ml Asynchronous programming 5