/// <summary> /// Resolve an IP address or host name, and return the result synchronously /// through the delegate passed in by the caller. /// </summary> /// <param name="host">The IP address or host name to resolve</param> /// <param name="callbackDelegate">The delegate wrapping the callback method</param> internal void Resolve(string host, ResolveCallbackDelegate callbackDelegate) { // This performs the same task as above, but instead of calling back // through an interface, a delegate is used to inform the caller of the // results. callbackDelegate(Dns.Resolve(host)); }
/// <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); } }