c# 等待异步委托结果的三种方式
2014-07-26 10:31
495 查看
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace t_20_01
{
//声明委托类型
public delegate int TakesWhileDelegate(int data,int ms);
class Program
{
static void Main(string[] args)
{
//等待委托的结果
#region 第一种执行操作
TakesWhileDelegate d1=TakesWhile;
//开始一部调用
IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);
//未完成是的操作
while (!ar.IsCompleted)
{
Console.Write(".");
Thread.Sleep(50);
}
//完成后调用该方法 获取返回结果
int result = d1.EndInvoke(ar);
Console.WriteLine("result:{0}", result);
#endregion
#region 第二种执行操作
TakesWhileDelegate d2 = TakesWhile;
IAsyncResult ar2 = d2.BeginInvoke(1, 3000, null, null);
while (true)
{
Console.Write(".");
// waitone 超时时 循环继续执行
if (ar.AsyncWaitHandle.WaitOne(50, false))
{
Console.WriteLine("can get result now");
break;
}
}
int result2 = d2.EndInvoke(ar2);
Console.WriteLine("result2:{0}",result2);
#endregion
#region 第三种执行操作 回掉函数
TakesWhileDelegate d3 = TakesWhile;
d3.BeginInvoke(1, 3000, TakesAwhileCompleted, d3);
for (int i = 0; i < 100; i++)
{
Console.Write(".{0}",i);
Thread.Sleep(50);
}
#endregion
Console.ReadKey();
}
static void TakesAwhileCompleted(IAsyncResult ar)
{
if (ar == null) throw new ArgumentNullException("ar");
TakesWhileDelegate d3 = ar.AsyncState as TakesWhileDelegate;
Trace.Assert(d3 != null, "Invalid object type");
int result = d3.EndInvoke(ar);
Console.WriteLine("result:{0}",result);
}
static int TakesWhile(int data, int ms)
{
Console.WriteLine("takesAwhile started");
Thread.Sleep(ms);
Console.WriteLine("takesAwhile completed");
return ++data;
}
}
}
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace t_20_01
{
//声明委托类型
public delegate int TakesWhileDelegate(int data,int ms);
class Program
{
static void Main(string[] args)
{
//等待委托的结果
#region 第一种执行操作
TakesWhileDelegate d1=TakesWhile;
//开始一部调用
IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);
//未完成是的操作
while (!ar.IsCompleted)
{
Console.Write(".");
Thread.Sleep(50);
}
//完成后调用该方法 获取返回结果
int result = d1.EndInvoke(ar);
Console.WriteLine("result:{0}", result);
#endregion
#region 第二种执行操作
TakesWhileDelegate d2 = TakesWhile;
IAsyncResult ar2 = d2.BeginInvoke(1, 3000, null, null);
while (true)
{
Console.Write(".");
// waitone 超时时 循环继续执行
if (ar.AsyncWaitHandle.WaitOne(50, false))
{
Console.WriteLine("can get result now");
break;
}
}
int result2 = d2.EndInvoke(ar2);
Console.WriteLine("result2:{0}",result2);
#endregion
#region 第三种执行操作 回掉函数
TakesWhileDelegate d3 = TakesWhile;
d3.BeginInvoke(1, 3000, TakesAwhileCompleted, d3);
for (int i = 0; i < 100; i++)
{
Console.Write(".{0}",i);
Thread.Sleep(50);
}
#endregion
Console.ReadKey();
}
static void TakesAwhileCompleted(IAsyncResult ar)
{
if (ar == null) throw new ArgumentNullException("ar");
TakesWhileDelegate d3 = ar.AsyncState as TakesWhileDelegate;
Trace.Assert(d3 != null, "Invalid object type");
int result = d3.EndInvoke(ar);
Console.WriteLine("result:{0}",result);
}
static int TakesWhile(int data, int ms)
{
Console.WriteLine("takesAwhile started");
Thread.Sleep(ms);
Console.WriteLine("takesAwhile completed");
return ++data;
}
}
}
相关文章推荐
- [深入学习C#]完成异步委托的三种方式
- [深入学习C#]完成异步委托的三种方式
- C# 委托的三种调用示例(同步调用 异步调用 异步回调)
- C# 创建线程的简单方式:异步委托 .
- 三种实例化委托的方式(C# 编程指南)
- C# 委托的三种调用示例(同步调用 异步调用 异步回调) 之 FUNC 版
- C# 委托的三种调用示例(同步调用 异步调用 异步回调)
- C# 创建线程的简单方式:异步委托
- C#中的三种委托方式:Func委托,Action委托,Predicate委托
- 委托(delegate)的三种调用方式:同步调用,异步调用,异步回调(转载)
- C# 通过委托+异步的方式约束函数的执行超时时间
- Unity之C#——异步委托开启线程,三种方法检测结束
- C# 委托的三种调用示例(同步调用 异步调用 异步回调)
- c# 委托和事件理解 三种方式
- 等待异步委托的方式
- C#中的三种委托方式:Func委托,Action委托,Predicate委托
- C# 委托的三种调用示例(同步调用 异步调用 异步回调)
- 20160711--C# 委托的三种调用示例(同步调用 异步调用 异步回调)【转载】
- SCA编程模式三种方式的异步调用
- C#中委托的最简单理解方式