异步委托调用和回调函数的结合使用
来源:互联网 发布:python turtle 国旗 编辑:程序博客网 时间:2024/04/26 13:51
using System;
using System.Runtime.Remoting.Messaging;
namespace ConsoleApplication17
{
public delegate string MyDelegate();
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
Class1 c1=new Class1();
MyDelegate d=new MyDelegate(c1.M);
Console.WriteLine("Start...");
//异步委托调用
d.BeginInvoke(new System.AsyncCallback(c1.CallBackFun),"heihei");
//程序会继续执行
Console.WriteLine("going next...");
Console.ReadLine();
}
public void CallBackFun(IAsyncResult res)
{
//获取所调用异步委托的最后一个参数,这个参数可以是一个
//自定义的参数类型
string arg=(string)res.AsyncState;
//将res强制转换从BeginInvoke获取的IAsyncResult
AsyncResult a=(AsyncResult)res;
//获取在其上调用异步调用的委托对象
MyDelegate temp=(MyDelegate)a.AsyncDelegate;
//获取异步委托调用的返回值
string s=(string)temp.EndInvoke(res);
//打印
Console.WriteLine("the last arg from the delegate is {0}",arg);
Console.WriteLine("the string return from the M method is {0}",s);
}
public string M()
{
Console.WriteLine("do something slow...");
//返回值
return "good";
}
}
}
结果:
Start...
going next...
do something slow...
the last arg from the delegate is heihei
the string return from the M method is good
- 异步委托调用和回调函数的结合使用
- 异步调用和回调函数
- 异步调用和回调函数
- 同步调用、异步调用和回调函数
- 回调函数和委托
- 委托和回调函数
- 同步调用、回调函数、异步调用
- 黑马程序员_学习日记66_716ASP.NET(使用回调函数的异步委托、生产者消费者)
- Effective JavaScript Item 62 在异步调用中使用嵌套或者命名的回调函数
- C#中委托、事件和回调函数的理解
- java 回调函数同步、异步调用
- C#中的委托(为什么C#调用dll的回调函数用委托)
- AJAX与回调函数的结合使用
- 入门 委托、事件和回调函数
- 分层---回调函数,回调接口,委托,异步所有事情的源头1
- 分层---回调函数,回调接口,委托,异步所有事情的源头2
- 调用机制:回调函数、同步调用、异步调用
- 委托的异步调用和同步调用
- 今日嵌品
- SOCKET UDP 通讯过程中 10022 和 10014 偶尔出现问题
- css学习1
- 如何使用mscomm串口控件在.net中编程
- js学习2
- 异步委托调用和回调函数的结合使用
- 快速生成100万不重复的8位编号
- Filter in JSP
- Linux解释器原理
- 表单提交时自动复制到剪贴板
- asp中类的使用笔记(1)
- Web services for RoR
- 无法显示隐藏文件和文件夹
- 海量数据库的查询优化及分页算法方案