使用AsyncCallback处理异步调用
2010-06-30 11:18
591 查看
using System.Threading;
using System;
using System.Runtime.Remoting.Messaging; //不要忘记该引用,AsyncResult(异布调用的结果)
namespace SyncDelegate
{
public delegate int BinaryOp(int x, int y);
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Synch Delegate Review *****");
Console.WriteLine("Main() invoked on thread {0}.",
Thread.CurrentThread.ManagedThreadId);
BinaryOp b = new BinaryOp(Add);
IAsyncResult iftAR= b.BeginInvoke(10, 10,new AsyncCallback(AddComplete),"Main() thanks you for adding these numbers"); //使用AsyncCallback实现异步回调:引用在异步操作完成时调用的回调方法。
Console.ReadLine();
}
static void AddComplete(IAsyncResult itfAR)//异步操作完成时执行的方法
{
Console.WriteLine("AddComplete() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("addition is complete");
AsyncResult ar = (AsyncResult)itfAR; //AsyncResult封装对委托的异步操作的结果
BinaryOp b = (BinaryOp)ar.AsyncDelegate;//AsyncDelegate获取原委托对象
Console.WriteLine("10+10 is {0}", b.EndInvoke(itfAR));
string msg = (string)itfAR.AsyncState;//AsyncState获取主线程传入的数据
Console.WriteLine(msg);
}
#region Very time consuming addition!
static int Add(int x, int y)
{
Console.WriteLine("Add() invoked on thread {0}.",
Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(3000);
return x + y;
}
#endregion
}
}
using System;
using System.Runtime.Remoting.Messaging; //不要忘记该引用,AsyncResult(异布调用的结果)
namespace SyncDelegate
{
public delegate int BinaryOp(int x, int y);
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Synch Delegate Review *****");
Console.WriteLine("Main() invoked on thread {0}.",
Thread.CurrentThread.ManagedThreadId);
BinaryOp b = new BinaryOp(Add);
IAsyncResult iftAR= b.BeginInvoke(10, 10,new AsyncCallback(AddComplete),"Main() thanks you for adding these numbers"); //使用AsyncCallback实现异步回调:引用在异步操作完成时调用的回调方法。
Console.ReadLine();
}
static void AddComplete(IAsyncResult itfAR)//异步操作完成时执行的方法
{
Console.WriteLine("AddComplete() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("addition is complete");
AsyncResult ar = (AsyncResult)itfAR; //AsyncResult封装对委托的异步操作的结果
BinaryOp b = (BinaryOp)ar.AsyncDelegate;//AsyncDelegate获取原委托对象
Console.WriteLine("10+10 is {0}", b.EndInvoke(itfAR));
string msg = (string)itfAR.AsyncState;//AsyncState获取主线程传入的数据
Console.WriteLine(msg);
}
#region Very time consuming addition!
static int Add(int x, int y)
{
Console.WriteLine("Add() invoked on thread {0}.",
Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(3000);
return x + y;
}
#endregion
}
}
相关文章推荐
- 使用 AsyncCallback 处理异步调用
- 【转】.NET 4.5 使用async和await关键字调用异步方法
- asp.net ashx 一般处理程序 使用async await异步直接 copy可用哦
- "现在已经正在使用此 SocketAsyncEventArgs 实例进行异步套接字操作"的处理
- 线程锁 线程并发处理 | 异步调用委托的使用
- Spring异步任务处理,@Async的配置和使用
- .NET 4.5 使用async和await关键字调用异步方法
- [置顶] Ajax程序:处理异步调用中的异常(使用Asp.Net Ajax内建的异常处理方法)
- Spring异步任务处理,@Async的配置和使用
- 基于委托的异步调用,一个简单的同步调用,以及对AsyncCallback,IAsyncResult,AsyncResult,BeginInvoke,EndInvoke的概述性解释
- Spring异步任务处理,@Async的配置和使用
- AsyncCallback BeginInvode endinvode 异步调用
- Spring Boot利用@Async异步调用:使用Future及定义超时详解
- Ajax程序:处理异步调用中的异常(使用Asp.Net Ajax内建的异常处理方法)
- Java并发编程:Spring中使用 @async 注解实现异步调用的原理
- .NET/C# 使用async和await关键字调用异步方法
- Spring异步任务处理,@Async的配置和使用
- 如何使用 servlet3.x的 async 异步处理功能
- Async.js用来处理异步的调用
- 19、构建多线程应用程序:委托异步调用方法(BeginInvoke,EndInvoke,AsyncCallback)