BT

New.NET Async Control Flow Explained

by David Cooksey on Apr 28, 2011 |

Alan Berman recently detailed the new .NET Async feature’s control flow. On the Visual Basic Team site he explained that the Async feature allows the calling code and the response code to be in the same place. This is possible due to the Async and Await keywords. Async marks a function as returning an asynchronous task and Await transfers control flow back to the calling function until a task has completed.

Use of an Async function requires two task objects; one for the function marked Async and another for the real asynchronous operation taking place. When Await is called on a task within an Async function control is instantly returned to the calling function. When the real asynchronous call completes, a new thread picks up execution after the Await call. Await cannot be called on a task outside of a function marked Async. The code below extends the example found on the Visual Basic Team Site.

Module Module1
 Sub Main()
  ShowInfo("In Main before Async call")
  Dim task As Task(Of String) = GetWebPageAsync("http://www.msdn.com/")
  ShowInfo("In Main after Async call", task.Id)
  Dim content As String = task.Result 'Pauses until the result is available (synchronous)
  ShowInfo("In Main after Async call has completed", task.Id)
  Console.ReadKey()
 End Sub

 Private Async Function GetWebPageAsync(ByVal webAddress As String) As Task(Of String)
  Dim theTask As Task(Of String) = New WebClient().DownloadStringTaskAsync(New Uri(webAddress))
  ShowInfo("In GetWebPageAsync before Await", theTask.Id)
  Dim result As String = Await theTask ' returns to the calling function
  ShowInfo("In GetWebPageAsync After Await", theTask.Id)
  Return result
 End Function

 Private Sub ShowInfo(ByVal msg As String, Optional taskId As Integer = 0)
  Console.WriteLine("Thread ID: " & System.Threading.Thread.CurrentThread.ManagedThreadId & " " & msg & IIf(taskId = 0, "", " task: " & taskId.ToString))
 End Sub
End Module

Sample Output:
Thread ID: 8 In Main before Async call
Thread ID: 8 In GetWebPageAsync before Await task: 1
Thread ID: 8 In Main after Async call task: 2
Thread ID: 13 In GetWebPageAsync After Await task: 1
Thread ID: 8 In Main after Async call has completed task: 2

Note that once Await is called, the primary thread is finished with the Async method. When the asynchronous web request completes, a different thread handles processing the remainder of the Async method. The main thread’s request for the result blocks until the Async method completes. This feature is available as part of the Async CTP for VB.NET and C#.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT