zoukankan
html css js c++ java
异步调用委托的3种方法
异步调用委托的3种方法
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
AsyncDelegate
{
class
Program
{
delegate
int
IntIntDelegate(
int
x);
//
生明一个委托
int
Square(
int
x)
{
return
x
*
x;
}
void
AsyncDelegateExample()
{
IntIntDelegate f
=
Square;
//
方法一
IAsyncResult ar1
=
f.BeginInvoke(
10
,
null
,
null
);
while
(
!
ar1.IsCompleted)
//
循环直到异步完成
Console.WriteLine(f.EndInvoke(ar1));
//
do some work
//
方法二
IAsyncResult ar2
=
f.BeginInvoke(
20
,
null
,
null
);
//
do some work
ar2.AsyncWaitHandle.WaitOne();
//
等待直到异步完成
Console.WriteLine(f.EndInvoke(ar2));
//
方法三 完成后调用回调函数,取得结束通知的结果
IAsyncResult ar3
=
f.BeginInvoke(
30
, AsyncDelegateCallback, f);
}
void
AsyncDelegateCallback(IAsyncResult ar)
//
回调函数
{
IntIntDelegate f
=
(IntIntDelegate)ar.AsyncState;
if
(ar.IsCompleted)
{
Console.WriteLine(f.EndInvoke(ar));
}
//
Console.WriteLine(f.EndInvoke(ar));
}
static
void
Main(
string
[] args)
{
Program test
=
new
Program();
test.AsyncDelegateExample();
//
客户端调用
}
}
}
输出: 100
400
900
查看全文
相关阅读:
顺序表(线性表)操作的思想及实现之C#版
基于CXF Java 搭建Web Service
敏捷方法 – 灵活,可靠的软件 使用设计模式和敏捷开发
使用HttpHanlder处理404: File not found
关于js模块加载的尝试
Tfs 自动部署 部署图
jQuery能做到,PHP能做到,C#也能做到
优化反射性能的总结(下)
推荐系统
10个前端开发必备的工具或使用方法
原文地址:https://www.cnblogs.com/xiaobaigang/p/931016.html
最新文章
池设计
iOS开发中常见问题集锦
让你的网站变成响应式的3个简单步骤
发一个英文阅读时,快捷辅助翻译工具
软件架构
网站后台权限设计
hadoop集群监控工具ambari安装
聊天室服务分析设计
Streaming live results to a web site using MSMQ/Duplex WCF/SignalR/jQuery
mvc4 截取上传图片做头像
热门文章
支持多线程的日志记录类实现
Windows 服务入门指南
OS之争:永不停歇的战争
.NET开源压缩组件介绍与入门
WPF邮件群发工具开发
不“简单”的HttpClient
.NET4.5之初识async与await
Spring.Net 如何管理您的类___自定义对象行为
委托和事件
cnblogs博文浏览
Copyright © 2011-2022 走看看