Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
EndResolve Method

Dns.EndResolve Method

Ends an asynchronous request for DNS information.

[Visual Basic]
Public Shared Function EndResolve( _
   ByVal asyncResult As IAsyncResult _
) As IPHostEntry
[C#]
public static IPHostEntry EndResolve(
 IAsyncResult asyncResult
);
[C++]
public: static IPHostEntry* EndResolve(
 IAsyncResult* asyncResult
);
[JScript]
public static function EndResolve(
   asyncResult : IAsyncResult
) : IPHostEntry;

Parameters

asyncResult
The pending request for DNS information.

Return Value

An IPHostEntry object that contains DNS information about a host.

Exceptions

Exception Type Condition
ArgumentNullException asyncResult is a null reference (Nothing in Visual Basic).

Remarks

The EndResolve method completes an asynchronous request for DNS information that was started with a call to BeginResolve.

Example

[Visual Basic, C#, C++] The following example ends an asynchronous request for DNS host information.

[Visual Basic] 
Class DnsBeginGetHostByName
    
    Class RequestState
        Public host As IPHostEntry
        
        Public Sub New()
            host = Nothing
        End Sub 'New
    End Class 'RequestState
    
    
    Public Shared Sub Main()
     Try
        ' Create an instance of the RequestState class.
        Dim myRequestState As New RequestState()
        
        ' Begin an asynchronous request for information such as the host name, IP addresses, 
        ' or aliases for the specified URI.
        Dim asyncResult As IAsyncResult = CType(Dns.BeginResolve("www.contoso.com", AddressOf RespCallback, myRequestState),IAsyncResult)

        ' Wait until asynchronous call completes.
        While asyncResult.IsCompleted <> True
        End While
        
        Console.WriteLine(("Host name : " + myRequestState.host.HostName))
        Console.WriteLine(ControlChars.Cr + "IP address list : ")
        Dim index As Integer
        For index = 0 To myRequestState.host.AddressList.Length - 1
            Console.WriteLine(myRequestState.host.AddressList(index))
        Next index
        Console.WriteLine(ControlChars.Cr + "Aliases : ")

        For index = 0 To myRequestState.host.Aliases.Length - 1
            Console.WriteLine(myRequestState.host.Aliases(index))
        Next index
    catch e as Exception
            Console.WriteLine("Exception caught!!!")
            Console.WriteLine(("Source : " + e.Source))
            Console.WriteLine(("Message : " + e.Message))
    End Try
    End Sub 'Main
    
    
    Private Shared Sub RespCallback(ar As IAsyncResult)
        Try
            ' Convert the IAsyncResult object to a RequestState object.
            Dim tempRequestState As RequestState = CType(ar.AsyncState, RequestState)

            ' End the asynchronous request.
            tempRequestState.host = Dns.EndResolve(ar)
            Catch e As ArgumentNullException
            Console.WriteLine("ArgumentNullException caught!!!")
            Console.WriteLine(("Source : " + e.Source))
            Console.WriteLine(("Message : " + e.Message))
            Catch e As Exception
            Console.WriteLine("Exception caught!!!")
            Console.WriteLine(("Source : " + e.Source))
            Console.WriteLine(("Message : " + e.Message))
        End Try
    End Sub 'RespCallback

[C#] 
class DnsBeginGetHostByName
{   
   public static System.Threading.ManualResetEvent allDone = null;

   class RequestState
   {
      public IPHostEntry host;
      public RequestState()
      {
         host = null;
      }
   }

   public static void Main()
   {
     allDone = new ManualResetEvent(false);
     // Create an instance of the RequestState class.
     RequestState myRequestState = new RequestState();

     // Begin an asynchronous request for information like host name, IP addresses, or 
     // aliases for specified the specified URI.
     IAsyncResult asyncResult = Dns.BeginResolve("www.contoso.com", new AsyncCallback(RespCallback), myRequestState );

     // Wait until asynchronous call completes.
     allDone.WaitOne();
     Console.WriteLine("Host name : " + myRequestState.host.HostName);
     Console.WriteLine("\nIP address list : ");
     for(int index=0; index < myRequestState.host.AddressList.Length; index++)
     {
       Console.WriteLine(myRequestState.host.AddressList[index]);
     }      
     Console.WriteLine("\nAliases : ");
     for(int index=0; index < myRequestState.host.Aliases.Length; index++)
     {
       Console.WriteLine(myRequestState.host.Aliases[index]);
     }      
   }
   
   private static void RespCallback(IAsyncResult ar)
   {
     try 
     {
       // Convert the IAsyncResult object to a RequestState object.
       RequestState tempRequestState = (RequestState)ar.AsyncState;
       // End the asynchronous request.
       tempRequestState.host = Dns.EndResolve(ar);
       allDone.Set();
     }
       catch(ArgumentNullException e) 
     {
       Console.WriteLine("ArgumentNullException caught!!!");
       Console.WriteLine("Source : " + e.Source);
       Console.WriteLine("Message : " + e.Message);
     }   
     catch(Exception e)
     {
       Console.WriteLine("Exception caught!!!");
       Console.WriteLine("Source : " + e.Source);
       Console.WriteLine("Message : " + e.Message);
     }
   }
}

[C++] 
public __gc class DnsBeginGetHostByName
{
public:
   static System::Threading::ManualResetEvent* allDone = 0;
   __gc class RequestState
   {
   public:
      IPHostEntry*  host;
      RequestState()
      {
         host = 0;
      }
   };

   static void RespCallback(IAsyncResult* ar)
   {
      try
      {
         // Convert the IAsyncResult* Object* to a RequestState Object*.
         RequestState* tempRequestState = 
            dynamic_cast<RequestState*>(ar->AsyncState);
         // End the asynchronous request.
         tempRequestState->host = Dns::EndResolve(ar);
         allDone->Set();
      }
      catch (ArgumentNullException* e)
      {
         Console::WriteLine(S"ArgumentNullException caught!!!");
         Console::WriteLine(S"Source : {0}", e->Source);
         Console::WriteLine(S"Message : {0}", e->Message);
      }
      catch (Exception* e)
      {
         Console::WriteLine(S"Exception caught!!!");
         Console::WriteLine(S"Source : {0}", e->Source);
         Console::WriteLine(S"Message : {0}", e->Message);
      }
   }
};

int main()
{
   DnsBeginGetHostByName::allDone = new ManualResetEvent(false);
   // Create an instance of the RequestState class.
   DnsBeginGetHostByName::RequestState* myRequestState = 
      new DnsBeginGetHostByName::RequestState();

   // Begin an asynchronous request for information like host name, IP addresses, or
   // aliases for specified the specified URI.
   IAsyncResult* asyncResult = Dns::BeginResolve(S"www.contoso.com", 
      new AsyncCallback(asyncResult, 
      DnsBeginGetHostByName::RespCallback), 
      myRequestState);

   // Wait until asynchronous call completes.
   DnsBeginGetHostByName::allDone->WaitOne();
   Console::WriteLine(S"Host name : {0}", myRequestState->host->HostName);
   Console::WriteLine(S"\nIP address list : ");
   for (int index=0; index < myRequestState->host->AddressList->Length; index++)
      Console::WriteLine(myRequestState->host->AddressList->Item[index]);
   Console::WriteLine(S"\nAliases : ");
   for (int index=0; index < myRequestState->host->Aliases->Length; index++)
      Console::WriteLine(myRequestState->host->Aliases->Item[index]);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Dns Class | Dns Members | System.Net Namespace | Including Asynchronous Calls

Show:
© 2015 Microsoft