Permalink
Fetching contributors…
Cannot retrieve contributors at this time
35 lines (31 sloc) 2.81 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
Using an AsyncCallback Delegate and State Object
03/30/2017
.net
dotnet-standard
article
asynchronous programming, delegates
AsyncCallback delegate, samples
asynchronous programming, state objects
IAsyncResult interface, samples
e3e5475d-c5e9-43f0-928e-d18df8ca1f1d
11
rpetrusha
ronpet
wpickett

Using an AsyncCallback Delegate and State Object

When you use an xref:System.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.

Example

The following code example demonstrates using asynchronous methods in the xref:System.Net.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 xref:System.Net.Dns.BeginGetHostByName%2A method. The ProcessDnsInformation method is called each time a request completes. The HostRequest object is retrieved using the xref:System.IAsyncResult.AsyncState%2A property. The ProcessDnsInformation method uses the HostRequest object to store the xref:System.Net.IPHostEntry returned by the request or a xref:System.Net.Sockets.SocketException thrown by the request. When all requests are complete, the application iterates over the HostRequest objects and displays the DNS information or xref:System.Net.Sockets.SocketException error message.

[!code-csharpAsyncDesignPattern#5] [!code-vbAsyncDesignPattern#5]

See Also

Event-based Asynchronous Pattern (EAP)
Event-based Asynchronous Pattern Overview
Using an AsyncCallback Delegate to End an Asynchronous Operation