/// <summary> /// Ends an asynchronous request for DNS information. /// </summary> /// <param name="AsyncResult"> /// An System.IAsyncResult instance that is returned by a call to the System.Net.Dns.BeginResolve(System.String,System.AsyncCallback,System.Object) /// method. /// </param> /// <returns>An System.Net.IPHostEntry object that contains DNS information about a host.</returns> public IPHostEntry EndResolve(IAsyncResult AsyncResult) { AsyncResult aResult = (AsyncResult)AsyncResult; ResolveDelegate g = (ResolveDelegate)aResult.AsyncDelegate; return(g.EndInvoke(AsyncResult)); }
private void AsyncCustomCallbackMethod(IAsyncResult ar) { // If any exceptions are raised by the called method, they won't // be thrown until the results are obtained. try { // Unwrap the delegate so that the EndInvoke method can be called. ResolveDelegate synchMethod = (ResolveDelegate)((System.Runtime.Remoting.Messaging.AsyncResult)ar).AsyncDelegate; IPHostEntry hostInformation = synchMethod.EndInvoke(ar); DisplayResults(hostInformation); } catch (System.Net.Sockets.SocketException) { Console.WriteLine("Bad host name (SocketException)"); resultsDisplayed = true; } }
} // BeginResolve /// <include file='doc\DNS.uex' path='docs/doc[@for="Dns.EndResolve"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public static IPHostEntry EndResolve(IAsyncResult asyncResult) { // // parameter validation // if (asyncResult == null) { throw new ArgumentNullException("asyncResult"); } GlobalLog.Print("Dns.EndResolve"); if (!asyncResult.IsCompleted) { asyncResult.AsyncWaitHandle.WaitOne(); } return(resolve.EndInvoke(asyncResult)); } // EndResolve()
/// <summary> /// This method is called by the Main static method to perform the /// DNS resolutions requested by the user demonstrating the callback /// options specified. /// </summary> /// <param name="host">An IP address or host name to resolve</param> /// <param name="methodToUse">The callback option to demonstrate</param> private void DoResolve(string host, CallbackOption methodToUse) { resultsDisplayed = false; IPHostEntry hostInformation = null; Resolver resolver = new Resolver(); switch (methodToUse) { case CallbackOption.UseInterface: { Console.WriteLine("Resolving..."); // By passing an interface that this object supports, the called // method can notify this object of the results try { resolver.Resolve(host, (IResolveCallback)this); } catch (System.Net.Sockets.SocketException) { Console.WriteLine("Bad host name (SocketException)"); resultsDisplayed = true; } break; } case CallbackOption.UseSynchronousDelegate: { Console.WriteLine("Resolving..."); // By passing a delegate wrapping the HostResolved method, the // called object can notify this object of the result in a synchronous // or asynchronous manner, depending on how it is constructed. ResolveCallbackDelegate cb = new ResolveCallbackDelegate(HostResolved); resolver.Resolve(host, cb); break; } case CallbackOption.UseAsynchronousDelegateWithWait: { Console.Write("Resolving"); // By wrapping a synchronous long-running method (DNS resolution) // with a delegate, this object can call that method // asynchronously and show progress (in this case) or do other // work while it executes. In this scenario, it waits on the // result using the WaitHandle provided by IAsyncResult. ResolveDelegate synchMethod = new ResolveDelegate(resolver.Resolve); // The BeginInvoke method is supplied by the C# compiler... // The IntelliSense engine does not display this at design time. IAsyncResult ar = synchMethod.BeginInvoke(host, null, null); // Write another period for each 100ms interval of wait time. while (!ar.AsyncWaitHandle.WaitOne(100, false)) { Console.Write("."); } Console.WriteLine(); // If any exceptions are raised by the called method, they won't // be thrown until the results are obtained. try { // The EndInvoke method is supplied by the C# compiler... // The IntelliSense engine does not display this at design time. hostInformation = synchMethod.EndInvoke(ar); DisplayResults(hostInformation); } catch (System.Net.Sockets.SocketException) { Console.WriteLine("Bad host name (SocketException)"); resultsDisplayed = true; } break; } case CallbackOption.UseAsynchronousDelegateWithCallback: { Console.WriteLine("Resolving..."); ResolveDelegate synchMethod = new ResolveDelegate(resolver.Resolve); AsyncCallback cb = new AsyncCallback(this.AsyncCustomCallbackMethod); // Begin the method's execution...when finished, the callback will be // called. IAsyncResult ar = synchMethod.BeginInvoke(host, cb, null); break; } case CallbackOption.UseFrameworkSuppliedSynchronousMethod: { Console.WriteLine("Resolving..."); // This calls the synchronous version of a framework-defined class // that also explicitly supports asynchronous invocation. try { hostInformation = Dns.Resolve(host); DisplayResults(hostInformation); } catch (System.Net.Sockets.SocketException) { Console.WriteLine("Bad host name (SocketException)"); resultsDisplayed = true; } break; } case CallbackOption.UseFrameworkSuppliedAsynchronousMethodWithWait: { Console.Write("Resolving"); IAsyncResult ar = Dns.BeginResolve(host, null, null); // Write another period for each 100ms interval of wait time. while (!ar.AsyncWaitHandle.WaitOne(100, false)) { Console.Write("."); } Console.WriteLine(); // If any exceptions are raised by the called method, they won't // be thrown until the results are obtained. try { hostInformation = Dns.EndResolve(ar); DisplayResults(hostInformation); } catch (System.Net.Sockets.SocketException) { Console.WriteLine("Bad host name (SocketException)"); resultsDisplayed = true; } break; } case CallbackOption.UseFrameworkSuppliedAsynchronousMethodWithCallback: { Console.WriteLine("Resolving..."); AsyncCallback cb = new AsyncCallback(this.AsyncFrameworkCallbackMethod); // Begin the call...when it is finished, the callback method will be // called. IAsyncResult ar = Dns.BeginResolve(host, cb, null); break; } default: Console.WriteLine("Not Implemented Yet"); break; } // If this method ends now, there is no guarantee that the host information // will be displayed before the next prompt to the user for more hosts to // resolve is shown. In order to force the wait, put the thread to sleep // for 100ms intervals until the output has been displayed. while (!resultsDisplayed) { // For the synchronous options, this will never get executed // because the results will have been displayed before execution // reaches this point. System.Threading.Thread.Sleep(100); } }