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#多线程之线程池篇中,我们将学习多线程访问共享资源的一些通用的技术,我们将学习到以下知识点: 在线程池中调用委托 在线程池中执行异步操作 线程池和并行度 实现取消选项 使用等待句柄和超时 使用计时 ...
随机推荐
- vue Watcher分类 computed watch
1.Watcher构造函数源码部分代码 if (options) { this.deep = !!options.deep this.user = !!options.user this.lazy = ...
- UsageLog4j
迁移时间:2017年5月21日09:42:46CreateTime--2017年1月2日09:35:55Author:Marydon原文链接:http://www.360doc.com/conte ...
- OpenWrt的开机启动服务(init scripts)
参考 https://wiki.openwrt.org/doc/techref/initscripts 以一个简单的例子来说明 #!/bin/sh /etc/rc.common # Example s ...
- Java多线程中run(), start(), join(), wait(), yield(), sleep()的使用
Run 每个Thread中需要实现的方法, 如果直接调用的话, 会是和单线程一样的效果, 要另起线程需要使用start(). start 新起线程调用run(). 主线程不等待直接往下执行 Yield ...
- CSS3饼状loading效果
概述 之前看到很多饼状loading效果是用图片的方式实现的,本例子采用的是纯CSS3实现,这样可以节省资源空间,有兴趣的小伙伴可以看下~ 详细 代码下载:http://www.demodashi.c ...
- 【mysql】mysql中单列索引、联合索引、Join联表查询建立索引 和 EXPLAIN的分析使用
2.创建联合索引,从坐到右分别为:userid.openId.name 2. #### -------------- 多表联合查询 update 2019/03/13 ------------ ...
- 微信小程序的POST和GET请求方式的header区别
1.post请求: wx.request({ url: 'https://m.***.com/index.php/Home/Xiaoxxf/make_order', header: { "C ...
- 单节点k8s的一个小例子 webapp+mysql
安装kubernetes 准备一台centos7 1) 关闭firewalld 和 selinux systemctl stop firewalld systemctl disable firewal ...
- windows批处理文件打印幻方
无论是批处理文件还是shell都是没有意义的,它们只是一种工具,并且是非常低级难懂的工具. 如果不会,那就保持不会就好了.不要花费太多时间在这些没意义的事情上. 批处理的没意义体现在: 难以表达 随便 ...
- 【LeetCode】135. Candy
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...