/// <include file='doc\DNS.uex' path='docs/doc[@for="Dns.BeginResolve"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public static IAsyncResult BeginResolve(string hostName, AsyncCallback requestCallback, object stateObject) { // // demand Unrestricted DnsPermission for this call // s_DnsPermission.Demand(); if (hostName == null) { throw new ArgumentNullException("hostName"); } GlobalLog.Print("Dns.BeginResolve: " + hostName); return(resolve.BeginInvoke(hostName, requestCallback, stateObject)); } // BeginResolve
/// <summary> /// Begins an asynchronous request to resolve a DNS host name or IP address to /// an System.Net.IPAddress instance. /// </summary> /// <param name="hostName">The DNS name of the host.</param> /// <param name="requestCallback"> /// An System.AsyncCallback delegate that references the method to invoke when /// the operation is complete. /// </param> /// <param name="stateObject"> /// A user-defined object that contains information about the operation. This /// object is passed to the requestCallback delegate when the operation is complete. /// </param> /// <returns>An System.IAsyncResult instance that references the asynchronous request.</returns> public IAsyncResult BeginResolve(string hostName, AsyncCallback requestCallback, object stateObject) { ResolveDelegate g = new ResolveDelegate(Resolve); return(g.BeginInvoke(hostName, requestCallback, stateObject)); }
/// <summary> /// Begins an asynchronous request to resolve a DNS host name or IP address to /// an System.Net.IPAddress instance. /// </summary> /// <param name="hostName">The DNS name of the host.</param> /// <param name="requestCallback"> /// An System.AsyncCallback delegate that references the method to invoke when /// the operation is complete. /// </param> /// <param name="stateObject"> /// A user-defined object that contains information about the operation. This /// object is passed to the requestCallback delegate when the operation is complete. /// </param> /// <returns>An System.IAsyncResult instance that references the asynchronous request.</returns> public IAsyncResult BeginResolve(string hostName, AsyncCallback requestCallback, object stateObject) { ResolveDelegate g = new ResolveDelegate(Resolve); return g.BeginInvoke(hostName, requestCallback, stateObject); }
/// <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); } }