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. 关于"引用"的几点说明

    一.引用的基本知识 引用就是某一变量(目标)的一个别名,对引用的操作与对变量直接操作完全一样.引用的声明方法:类型标识符 &引用名=目标变量名: 说明: (1)&在此不是求地址运算,而 ...

  2. 从官方的BZR源安装avant-window-navigator

    资料来自: http://blog.163.com/azhai@126/blog/static/111056312008315842433/http://www.ibentu.org/2007/09/ ...

  3. 〖Ruby〗Ruby关键字

    模块定义:module 类定义:class 方法定义:def, undef 检查类型:defined? 条件语句:if, then, else, elsif, case, when, unless 循 ...

  4. c#实现统计代码运行时间

    方法一: //实例化一个计时器 Stopwatch watch = new Stopwatch(); //開始计时 watch.Start(); //此处为要计算的执行代码 for (int i = ...

  5. JSP常用跳转方式

      常用的跳转方式有以下几种: (1)href超链接标记,属于客户端跳转 (2)使用JavaScript完成,属于客户端跳转 (3)提交表单完成跳转,属于客户端跳转 (4)使用response对象,属 ...

  6. python之smtplib模块 发送邮件

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #smtplib模块 发送邮件 import smtplib from email.mime.text imp ...

  7. HighCharts画时间趋势图,标示区以及点击事件操作

    最近在用HighCharts画趋势图,如果按照设计文档上来画那太复杂了,于是根据自己多年的经验改动了设计文档,添加了highcharts的标示区,然而我也发现,最后一次画highchart趋势图还是在 ...

  8. PHP基于Sphinx+Swcs中文分词的全文的检索

    简介 Sphinx是开源的搜索引擎,它支持英文的全文检索.所以如果单独搭建Sphinx,你就已经可以使用全文索引了 但是有些时候我们还要进行中文分词所有scws就出现了,我们也可以使用Coreseek ...

  9. aop注解 事例

    spring.xml中aop的配置 <!-- aop的配置 --> <aop:config> <!-- 切入点表达式 --> <aop:pointcut ex ...

  10. java 安装配置时出现的问题

    Error: could not open `C:\Program Files\Java\jre6\lib\i386\jvm.cfg') jdkerror  前些日子装了个jdk7试了试,后来做项目需 ...