Polling for the Status of an Asynchronous Operation 

Applications that can do other work while waiting for the results of an asynchronous operation should not block waiting until the operation completes. Use one of the following options to continue executing instructions while waiting for an asynchronous operation to complete:


The following code example demonstrates using asynchronous methods in the Dns class to retrieve Domain Name System information for a user-specified computer. This example starts the asynchronous operation and then prints periods (".") at the console until the operation is complete. Note that null (Nothing in Visual Basic) is passed for the BeginGetHostByName AsyncCallback and Object parameters because these arguments are not required when using this approach.

'The following example demonstrates using asynchronous methods to
'get Domain Name System information for the specified host computer.
'This example polls to detect the end of the asynchronous operation.

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading

Namespace Examples.AdvancedProgramming.AsynchronousOperations

    Public Class PollUntilOperationCompletes
        Shared Sub UpdateUserInterface()
            ' Print a period to indicate that the application
            ' is still working on the request.
        End Sub
        Public Shared Sub Main(args() as String)
            ' Make sure the caller supplied a host name.
            If(args.Length = 0)
                ' Print a message and exit.
                Console.WriteLine("You must specify the name of a host computer.")
            End If
            ' Start the asynchronous request for DNS information.
            Dim result as IAsyncResult= Dns.BeginGetHostEntry(args(0), Nothing, Nothing)
            Console.WriteLine("Processing request for information...")
            ' Poll for completion information.
            ' Print periods (".") until the operation completes.
            Do while result.IsCompleted <> True
            ' The operation is complete. Process the results.
            ' Print a new line.
                Dim host as IPHostEntry = Dns.EndGetHostEntry(result)
                Dim  aliases() as String = host.Aliases
                Dim addresses() as IPAddress = host.AddressList
                Dim i as Integer
                If aliases.Length > 0
                    For i = 0 To aliases.Length -1 
                        Console.WriteLine("{0}", aliases(i))
                    Next i
                End If
                If addresses.Length > 0
                    For i = 0 To addresses.Length -1
                        Console.WriteLine("{0}", addresses(i).ToString())
                    Next i
                End If
            Catch e as SocketException
                  Console.WriteLine("An exception occurred while processing the request: {0}", e.Message)
            End Try
        End Sub
    End Class
End Namespace

See Also

Community Additions