Permalink
Fetching contributors…
Cannot retrieve contributors at this time
42 lines (35 sloc) 3.12 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 to End an Asynchronous Operation
03/30/2017
.net
dotnet-standard
article
ending asynchronous operations
asynchronous programming, ending operations
AsyncCallback delegate
stopping asynchronous operations
9d97206c-8917-406c-8961-7d0909d84eeb
7
rpetrusha
ronpet
wpickett

Using an AsyncCallback Delegate to End an Asynchronous Operation

Applications that can do other work while waiting for the results of an asynchronous operation should not block waiting until the operation completes. Use one of the following options to continue executing instructions while waiting for an asynchronous operation to complete:

  • Use an xref:System.AsyncCallback delegate to process the results of the asynchronous operation in a separate thread. This approach is demonstrated in this topic.

  • Use the xref:System.IAsyncResult.IsCompleted%2A property of the xref:System.IAsyncResult returned by the asynchronous operation's Begin OperationName method to determine whether the operation has completed. For an example that demonstrates this approach, see Polling for the Status of 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 creates an xref:System.AsyncCallback delegate that references the ProcessDnsInformation method. This method is called once for each asynchronous request for DNS information.

Note that the user-specified host is passed to the xref:System.Net.Dns.BeginGetHostByName%2Axref:System.Object parameter. For an example that demonstrates defining and using a more complex state object, see Using an AsyncCallback Delegate and State Object.

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

See Also

Event-based Asynchronous Pattern (EAP)
Event-based Asynchronous Pattern Overview
Calling Asynchronous Methods Using IAsyncResult
Using an AsyncCallback Delegate and State Object