Skip to content

Instantly share code, notes, and snippets.

Created October 28, 2010 06:43
What would you like to do?
The VBTeam leaked information about
We're very happy to announce today the Async CTP for Visual Basic and C#.
Async CTP homepage:
Async discussion forums:
Asynchronous programming is something that helps make your UI more responsive, especially in applications that interact with databases or network or disk. It's also used to make ASP servers scale better.
Until now, asynchronous programming has also been prohibitively hard, "not worth the investment"-hard. But with the Async CTP we've made asynchronous programming easy enough that developers should routinely consider asynchrony for most of their applications.
Async is a true "parity" feature between VB and C#. We had the same designers develop it in VB and C# at the same time, and the same developers implement the feature for both compilers, and even the same unit tests for both. The end result is a feature that's keyword-for-keyword identical in both languages (and probably bug-for-bug compatible in the CTP...)
We're also very happy to announce that iterators are coming to VB, and are included in the Async CTP! That's because async and iterators are deeply related (and inside the compiler they're implemented with 99% the same codebase). We'll write further posts about iterators.
A real-world example of asynchrony
"A waiter’s job is to wait on a table until the patrons have finished their meal.If you want to serve two tables concurrently, you must hire two waiters."
Q. That sentence is obviously wrong. What is the flaw?
A. You don't need two waiters! A single waiter can easily serve two tables concurrently, just by switching between them.
Why am I talking about waiters? Think of the waiter as a thread, and the table as a method that it executes. Traditionally, when you wanted to do two things at the same time (e.g. keep the UI responsive while downloading a file) then you'd have used two threads. But it's inefficient to create extra threads (hire waiters), and you had to jump through hoops to update the UI from the background thread (make sure that the waiters don't conflict).
Let's see what the waiter really does, written using VB Async:
Async Function WaitOnTablesAsync() As System.Threading.Tasks.Task(Of Decimal)
Dim takings = 0D
While Not TimeToGoHome
Await GetOrdersAsync()
Await WaitForKitchenAsync()
Await WaitForPatronsToFinish()
takings += Await GetPayment()
End While
Return takings
End Function
Private Async Sub Button1_Click() Handles Button1.Click
Dim takings = Await WaitOnTablesAsync()
MessageBox.Show("Shift finished; took {0}", takings)
End Sub
Things to notice in this code:
At each Await keyword, the waiter (thread) suspends execution of the method he's currently in. He can go off to do other work, like responding to UI events. When the thing he's awaiting has completed, and he's free, then he resumes his method.
WaitOnTables has the modifier Async. This modifier allows it to have Await expressions inside.
WaitOnTables returns a Task(Of Decimal). It actually returns the task promptly at its first await expression. This task represents the ongoing work that the waiter will do: when his shift finishes, when the While loop ends, the task will be marked as Completed and the final value returned to the caller.
Button1_Click can Await that Task. Once again, when this task has completed, then Button1_Click can resume and it shows the message-box.
What is asynchrony?
Asynchrony means "not [a-] at the same time [-synchronous]". It means that the the async function gives back a Task immediately, but that Task is just a placeholder: the real result will be delivered sometime later.
Asynchrony does not mean "running on a background thread". Running on a background thread is one way to implement asynchrony, but it's not the only way, and it's often not the best way.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment