IAsyncResult 接口由包含可异步操作的方法的类实现。它是启动异步操作的方法的返回类型,如 FileStream.BeginRead,也是结束异步操作的方法的第三个参数的类型,如 FileStream.EndRead。当异步操作完成时,IAsyncResult 对象也将传递给由 AsyncCallback 委托调用的方法。

支持 IAsyncResult 接口的对象存储异步操作的状态信息,并提供同步对象以允许线程在操作完成时终止。

有关如何使用 IAsyncResult 接口的详细说明,请参见“使用异步方式调用同步方法”主题。

using System;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging; //
// Context-Bound type with Synchronization Context Attribute
//
[Synchronization()]
public class SampleSyncronized : ContextBoundObject
{
// A method that does some work - returns the square of the given number
public int Square(int i)
{
Console.Write("SampleSyncronized.Square called. ");
Console.WriteLine("The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode());
return i*i;
}
} //
// Async delegate used to call a method with this signature asynchronously
//
public delegate int SampSyncSqrDelegate(int i); //Main sample class
public class AsyncResultSample
{
public static void Main()
{
int callParameter = ;
int callResult = ; //Create an instance of a context-bound type SampleSynchronized
//Because SampleSynchronized is context-bound, the object sampSyncObj
//is a transparent proxy
SampleSyncronized sampSyncObj = new SampleSyncronized(); //call the method synchronously
Console.Write("Making a synchronous call on the object. ");
Console.WriteLine("The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode());
callParameter = ;
callResult = sampSyncObj.Square(callParameter);
Console.WriteLine("Result of calling sampSyncObj.Square with {0} is {1}.\n\n", callParameter, callResult); //call the method asynchronously
Console.Write("Making an asynchronous call on the object. ");
Console.WriteLine("The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode());
SampSyncSqrDelegate sampleDelegate = new SampSyncSqrDelegate(sampSyncObj.Square);
callParameter = ; IAsyncResult aResult = sampleDelegate.BeginInvoke(callParameter, null, null); //Wait for the call to complete
aResult.AsyncWaitHandle.WaitOne(); callResult = sampleDelegate.EndInvoke(aResult);
Console.WriteLine("Result of calling sampSyncObj.Square with {0} is {1}.", callParameter, callResult);
}
}

IAsyncResult 接口的更多相关文章

  1. 异步编程(AsyncCallback委托,IAsyncResult接口,BeginInvoke方法,EndInvoke方法的使用小总结)

    http://www.cnblogs.com/panjun-Donet/archive/2009/03/03/1284700.html 让我们来看看同步异步的区别: 同步方法调用在程序继续执行之前需要 ...

  2. IAsyncResult 接口异步 和匿名委托

    IAsyncResult 接口异步 DataSet ds = new DataSet(); Mydelegate del = new Mydelegate(LoadData); IAsyncResul ...

  3. IAsyncResult接口

    #region 程序集 mscorlib.dll, v4.0.0.0 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framewor ...

  4. 异步核心接口IAsyncResult的实现

    要实现异步编程,就需要正确的实现IAsyncResult接口.IAsyncResult共有四个属性: public interface IAsyncResult { object AsyncState ...

  5. asp.net web 通过IHttpAsyncHandler接口进行消息推送

    .消息类,可直接通过这个类推送消息 HttpMessages using System; using System.Collections.Generic; using System.Linq; us ...

  6. C#中的异步调用及异步设计模式(二)——基于 IAsyncResult 的异步设计模式

    三.基于 IAsyncResult 的异步设计模式(设计层面) IAsyncResult 异步设计模式通过名为 BeginOperationName 和 EndOperationName 的两个方法来 ...

  7. C#异步编程模式IAsyncResult

    IAsyncResult 异步设计模式通过名为 BeginOperationName 和 EndOperationName 的两个方法来实现原同步方法的异步调用,如 FileStream 类提供了 B ...

  8. C#异步编程(二)

    async和await结构 序 前篇博客异步编程系列(一) 已经介绍了何谓异步编程,这篇主要介绍怎么实现异步编程,主要通过C#5.0引入的async/await来实现. BeginInvoke和End ...

  9. C#多线程之线程池篇1

    在C#多线程之线程池篇中,我们将学习多线程访问共享资源的一些通用的技术,我们将学习到以下知识点: 在线程池中调用委托 在线程池中执行异步操作 线程池和并行度 实现取消选项 使用等待句柄和超时 使用计时 ...

随机推荐

  1. Hibernate(六)一对多映射(多对一)

    一.Hinbernate中持久化类的关联关系 在数据库中,表表之间是通过外键关联的,在程序中是要转化为持久化类也就是(JAVA Bean)来实例的. 但在Hibernater中持久化的之间的映射关系, ...

  2. Java从零开始学二十六(包装类)

    一.包装类 包装类是将基本类型封装到一个类中.也就是将基本数据类型包装成一个类类型. java程序设计为每一种基本类型都提供了一个包装类.这些包装类就在java.lang包中.有8个包装类 二.包装类 ...

  3. TP自动生成模块目录

    TP自动生成模块目录 例如我想在项目中增加一个AdminI模块 只需要在入口文件index.php中添加: define('BIND_MODULE','Admin'); 再访问127.0.0.1项目就 ...

  4. IDEA开发web程序配置Tomcat

    1.下载zip版的Tomcat 7,并解压2.在IDEA中配置Tomcat 7 在idea中的Settings(Ctrl+Alt+s)(或者点击图标 ) 弹出窗口左上过滤栏中输入“Applicatio ...

  5. 光驱在资源管理器显示黄色感叹号的解决方法BIOS内有 系统下没有

    1. 点开“开始”-运行regedit.exe,进入注册表编辑器,到左边的项目栏里找到HKEY_LOCAL_MACHINE\SYSTEM \CurrentControlSet\Control\Clas ...

  6. 使用 C# 开发智能手机软件:推箱子(十二)

    这是"使用 C# 开发智能手机软件:推箱子"系列文章的第十二篇.在这篇文章中,介绍 Window/AboutDlg.cs 源程序文件. 这个源程序文件包括 AboutDlg 类,该 ...

  7. SettingsHBuilder

      迁移时间:2017年5月20日10:56:50CreateTime--2016年9月27日14:22:26Author:Marydon1.修改HBuilder开发工具快捷键工具-->首选项- ...

  8. Javascript模块化编程:require.js的用法

    摘自:http://blog.jobbole.com/30046/ 这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库 ...

  9. MONyog_5.6.9.0 key激活|监控MYSQL

    SQLyog与MONyog是一家公司对mysql推出的商业化软件,可能大家对SQLyog很熟悉,MONyog是对mysql-server服务的监控. 脚本执行时长.安全性.等的监控! key:a668 ...

  10. you need to know those webs !

    J2me开发网 http://www.j2medev.com/bbs/index.asp J2me社区 http://www.j2meforums.com/forum/ csdn http://www ...