This documentation is archived and is not being maintained.

Using an AsyncCallback Delegate and State Object

Updated: March 2012

When you use an AsyncCallback delegate to process the results of the asynchronous operation in a separate thread, you can use a state object to pass information between the callbacks and to retrieve a final result. This topic demonstrates that practice by expanding the example in Using an AsyncCallback Delegate to End an Asynchronous Operation.

The following code example demonstrates using asynchronous methods in the Dns class to retrieve Domain Name System (DNS) information for user-specified computers. This example defines and uses the HostRequest class to store state information. A HostRequest object gets created for each computer name entered by the user. This object is passed to the BeginGetHostByName method. The ProcessDnsInformation method is called each time a request completes. The HostRequest object is retrieved using the AsyncState property. The ProcessDnsInformation method uses the HostRequest object to store the IPHostEntry returned by the request or a SocketException thrown by the request. When all requests are complete, the application iterates over the HostRequest objects and displays the DNS information or SocketException error message.

No code example is currently available or this language may not be supported.

Date

History

Reason

March 2012

Corrected inaccurate introduction.

Customer feedback.

Show: