Using an AsyncCallback delegate to perform an asynchronous operation

The last few years at work have left me with very little time to keep up a blog.  Thankfully, things have finally settled down, leaving me with some quality time in the evening to pick back up my exploration of the CLR.  

Silverlight was the hot topic at this year’s Devlink, brand new buzzwords, wiz bang graphics, cool new techniques.  So to keep up with the Jones’s, we’ll be doing a deep dive on how to spin 3d ice cream cones to tune of Led Zepplin.  Unfortunately not, we’ll be diving into something rather more mundane, how to slough off work to background threads. Sorry.

We’ll start by taking a look at this article http://msdn.microsoft.com/en-us/library/ms228972.aspx which gives a pretty good example of using an AsyncCallback to perform an asynchronous operation.  Let’s pare this down into something a bit simpler, then walk through the code.

Imports System.Net

 

Module Module1

 

    Sub Main()

        Dim hostName = “mattsharp”

        Dim callback As System.AsyncCallback = AddressOf ProcessDnsInformation

        Dim state As New Object

        Console.WriteLine(“Before Begin”)

        Dns.BeginGetHostEntry(hostName, callback, state)

        Console.WriteLine(“After Begin”)

        Console.ReadLine()

    End Sub

 

    Sub ProcessDnsInformation(ByVal result As IAsyncResult)

        Dim host As IPHostEntry = Dns.EndGetHostEntry(result)

        Console.WriteLine(“IPAdress: “ & host.AddressList(0).ToString)

    End Sub

 

End Module

 

We declare the name of the computer we want to find the ip address for, the method to callback to when the work is complete, and a state object that can be used to pass state information to the callback method.  We then begin our asynchronous DNS lookup.

 

Dns.BeginGetHostEntry(hostName, callback, state)

 

And collect the results with the target sub procedure of our callback.

 

    Sub ProcessDnsInformation(ByVal result As IAsyncResult)

        Dim host As IPHostEntry = Dns.EndGetHostEntry(result)

        Console.WriteLine(“IPAdress: “ & host.AddressList(0).ToString)

    End Sub

 

When we run the program, we get the following output.

 

Proof that we’re not blocking while doing our lookup.  We can block if we want to by changing our code to use the synchronous GetHostEntry method.

Console.WriteLine(“Before Begin”)

Dim host As IPHostEntry = Dns.GetHostEntry(hostName)

Console.WriteLine(“IPAdress: “ & host.AddressList(0).ToString)

Console.WriteLine(“After Begin”)

 

Which leaves us with the opposite of our asynchronous operation.

 

 

 

So a basic, simple post to get back in the swing of things.  Next time we’ll look at using the state object to provide our callback method with state information.  And from there things will start to get much more interesting.  Promise.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>