IAsyncResult 接口
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 接口的更多相关文章
- 异步编程(AsyncCallback委托,IAsyncResult接口,BeginInvoke方法,EndInvoke方法的使用小总结)
http://www.cnblogs.com/panjun-Donet/archive/2009/03/03/1284700.html 让我们来看看同步异步的区别: 同步方法调用在程序继续执行之前需要 ...
- IAsyncResult 接口异步 和匿名委托
IAsyncResult 接口异步 DataSet ds = new DataSet(); Mydelegate del = new Mydelegate(LoadData); IAsyncResul ...
- IAsyncResult接口
#region 程序集 mscorlib.dll, v4.0.0.0 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framewor ...
- 异步核心接口IAsyncResult的实现
要实现异步编程,就需要正确的实现IAsyncResult接口.IAsyncResult共有四个属性: public interface IAsyncResult { object AsyncState ...
- asp.net web 通过IHttpAsyncHandler接口进行消息推送
.消息类,可直接通过这个类推送消息 HttpMessages using System; using System.Collections.Generic; using System.Linq; us ...
- C#中的异步调用及异步设计模式(二)——基于 IAsyncResult 的异步设计模式
三.基于 IAsyncResult 的异步设计模式(设计层面) IAsyncResult 异步设计模式通过名为 BeginOperationName 和 EndOperationName 的两个方法来 ...
- C#异步编程模式IAsyncResult
IAsyncResult 异步设计模式通过名为 BeginOperationName 和 EndOperationName 的两个方法来实现原同步方法的异步调用,如 FileStream 类提供了 B ...
- C#异步编程(二)
async和await结构 序 前篇博客异步编程系列(一) 已经介绍了何谓异步编程,这篇主要介绍怎么实现异步编程,主要通过C#5.0引入的async/await来实现. BeginInvoke和End ...
- C#多线程之线程池篇1
在C#多线程之线程池篇中,我们将学习多线程访问共享资源的一些通用的技术,我们将学习到以下知识点: 在线程池中调用委托 在线程池中执行异步操作 线程池和并行度 实现取消选项 使用等待句柄和超时 使用计时 ...
随机推荐
- 将 Shiro 作为应用的权限基础 二:shiro 认证
认证就是验证用户身份的过程.在认证过程中,用户需要提交实体信息(Principals)和凭据信息(Credentials)以检验用户是否合法.最常见的“实体/凭证”组合便是“用户名/密码”组合. 一. ...
- 借助AWR报告分析解决oracleCPU过高的问题(转)
原文地址:http://www.cnblogs.com/crystal-guoguo/p/4213458.html 简介:在oracle数据库中,有两个非常实用的自带监控工具EM(Enterprise ...
- Android——SD卡工具类——SDCardUtils.java
package com.xdsjs.save.utils; /** * SD卡相关的辅助类 * Created by xdsjs on 2015/10/13. */ import java.io.Fi ...
- 解压版MySQL安装后初始化root密码
1: C:\Users\gechong>mysql
- 在命令行上 Ubuntu 下使用 mutt 和 msmtp 发送 Gmail 邮件
在命令行写email from ubuntu 参考: http://www.habadog.com/2011/11/23/send-mail-with-msmtp-mutt-linux ...
- ubuntu 下安装配置open-iscsi并自动挂载
1.安装open-iscsi apt-get install open-iscsi 2.发现iscsi-target(ISCSI服务器IP:192.168.1.104) iscsiadm -m dis ...
- okhttp进行网络传输文件
其中使用了RXJava. public class HttpDataManager { private static HttpDataManager INSTANCE; private Request ...
- 1、redis之安装与配置
下载安装: redis-server.exe redis服务器的daemon启动程序 redis.conf redis配置文件 redis-cli.exe redis命令行操作工具.当然,也可以用te ...
- Windows I/O完成端口
内容: 1.基本概念 2.WINDOWS完成端口的特点 3.完成端口(Completion Ports )相关数据结构和创建 4.完成端口线程的工作原理 5.Windo ...
- Android API之java.lang.String
boolean java.lang.String.contains(CharSequence cs) String字符串是否包含CharSequence(cs).