Async Callback 作业

以下内容来自微软,用于学习参考

AsyncCallback Delegate

定义

引用在相应异步操作完成时调用的方法。

C#复制
[System.Runtime.InteropServices.ComVisible(true)]
[Serializable]
public delegate void AsyncCallback(IAsyncResult ar);
参数
ar
IAsyncResult

异步操作的结果。

继承
AsyncCallback
属性
ComVisibleAttribute SerializableAttribute

示例

下面的代码示例展示了如何使用 Dns 类中的异步方法,检索用户指定计算机的域名系统 (DNS) 信息。 此示例创建引用 ProcessDnsInformation 方法的 AsyncCallback 委托。 每次异步请求获取 DNS 信息,都会调用一次此方法。

C#复制
/*
The following example demonstrates using asynchronous methods to
get Domain Name System information for the specified host computers.
This example uses a delegate to obtain the results of each asynchronous 
operation.
*/

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading; using System.Collections.Specialized; using System.Collections; namespace Examples.AdvancedProgramming.AsynchronousOperations { public class UseDelegateForAsyncCallback { static int requestCounter; static ArrayList hostData = new ArrayList(); static StringCollection hostNames = new StringCollection(); static void UpdateUserInterface() { // Print a message to indicate that the application // is still working on the remaining requests. Console.WriteLine("{0} requests remaining.", requestCounter); } public static void Main() { // Create the delegate that will process the results of the // asynchronous request. AsyncCallback callBack = new AsyncCallback(ProcessDnsInformation); string host; do { Console.Write(" Enter the name of a host computer or <enter> to finish: "); host = Console.ReadLine(); if (host.Length > 0) { // Increment the request counter in a thread safe manner. Interlocked.Increment(ref requestCounter); // Start the asynchronous request for DNS information. Dns.BeginGetHostEntry(host, callBack, host); } } while (host.Length > 0); // The user has entered all of the host names for lookup. // Now wait until the threads complete. while (requestCounter > 0) { UpdateUserInterface(); } // Display the results. for (int i = 0; i< hostNames.Count; i++) { object data = hostData [i]; string message = data as string; // A SocketException was thrown. if (message != null) { Console.WriteLine("Request for {0} returned message: {1}", hostNames[i], message); continue; } // Get the results. IPHostEntry h = (IPHostEntry) data; string[] aliases = h.Aliases; IPAddress[] addresses = h.AddressList; if (aliases.Length > 0) { Console.WriteLine("Aliases for {0}", hostNames[i]); for (int j = 0; j < aliases.Length; j++) { Console.WriteLine("{0}", aliases[j]); } } if (addresses.Length > 0) { Console.WriteLine("Addresses for {0}", hostNames[i]); for (int k = 0; k < addresses.Length; k++) { Console.WriteLine("{0}",addresses[k].ToString()); } } } } // The following method is called when each asynchronous operation completes. static void ProcessDnsInformation(IAsyncResult result) { string hostName = (string) result.AsyncState; hostNames.Add(hostName); try { // Get the results. IPHostEntry host = Dns.EndGetHostEntry(result); hostData.Add(host); } // Store the exception message. catch (SocketException e) { hostData.Add(e.Message); } finally { // Decrement the request counter in a thread-safe manner. Interlocked.Decrement(ref requestCounter); } } } } 

注解

使用AsyncCallback委托处理单独线程中异步操作的结果。 AsyncCallback委托表示异步操作完成时调用的回调方法。 回调方法采用IAsyncResult参数,它随后用于获取异步操作的结果。

转载于:https://www.cnblogs.com/Stefanhouse/p/10053755.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值