Last time we looked at a simple example of using an AsyncCallback delegate to perform an asynchronous operation. We’ll build on this example and show how to pass some state into the callback.
Imports System.Net
Module Module2
Sub Main()
Dim hostName = “mattsharp”
Dim callback As System.AsyncCallback = AddressOf ProcessDnsInformation
Dim port As Integer = 99
Console.WriteLine(“Before Begin”)
Dns.BeginGetHostEntry(hostName, callback, port)
Console.WriteLine(“After Begin”)
Console.ReadLine()
End Sub
Sub ProcessDnsInformation(ByVal result As IAsyncResult)
Dim port As Integer = CType(result.AsyncState, Integer)
Dim host As IPHostEntry = Dns.EndGetHostEntry(result)
Dim point As New IPEndPoint(host.AddressList(0), port)
Console.WriteLine(“EndPoint: “ & point.ToString())
End Sub
End Module
All we’ve done is pass our current state, the port number we want to connect on, to our callback routine. When we run the program we get the following result.
So we’ve established that we can
1. Perform an operation asynchronously.
2. Process the result of the operation asynchronously.
3. Pass some state to the result of the operation.
More formally, we’ll start referring to this as a continuation. For a quick explanation of continuations, see this Wikipedia link, http://en.wikipedia.org/wiki/Continuation.
So we’ve added some pretty cool functionality to our visual basic bag of tricks. But, all is not well in the land of continuations. We’ll start looking at some of the problems next time.

