Action Func委托+异步委托
1.传统的委托的5种方式
2.Action和Func的委托
3.Action和Func的异步委托
如果对异步委托不太熟悉的朋友,建议先看看我的前几篇博文:
1. 《抛开书本谈 委托,为什么需要委托,它成就了什么?》
2. 《抛开书本,为什么出现了事件,事件与委托有什么渊源?》
3. 《委托与事件 在.net的争霸战 ,你选择了谁?(异步委托产生的原因)》
4. 《异步委托,恰似一江春水向东流(你了解的异步委托)》
开始进入正题,学习就像是 爬山,每天努力一点,你都会离顶峰近一点。
1.传统的委托
View Code
delegate string Dele( int i);
class Program
{
static void Main( string [] args)
{
// 委托初始化的5种方式
// 方法1
Dele d1 = new Dele(Speak);
d1( 10 );
// 方法2
Dele d2 = Speak;
d2( 20 );
// 方法3
Dele d3=( int i1)=>Speak(i1);
d3( 30 );
// 方法4
Dele d4 = ( int i) => { string str = string .Format( " The number is {0} " , i); Console.WriteLine(str); return str; };
d4( 40 );
// 方法5
Dele d5 = delegate ( int i) { string str = string .Format( " The number is {0} " , i); Console.WriteLine(str); return str; };
d5( 50 );
}
static string Speak( int i)
{
string str = string .Format( " The number is {0} " ,i);
Console.WriteLine(str);
return str;
}
}
输出:
The number is 10
The number is 20
The number is 30
The number is 40
The number is 50
2.Action委托
Action委托没有返回值的,而且Action<T>泛型委托最多是4个参数
View Code
class Program
{
static void Main( string [] args)
{
// Action委托初始化的5种方式
// 方法1
Action< int > action1 = new Action< int >(Speak);
action1( 10 );
// 方法2
Action< int > action2 = Speak;
action2( 20 );
// 方法3
Action< int > action3=( int i1)=>Speak(i1);
action3( 30 );
// 方法4
Action< int > action4 = ( int i) => { string str = string .Format( " The number is {0} " , i); Console.WriteLine(str); };
action4( 40 );
// 方法5
Action< int > action5 = delegate ( int i) { string str = string .Format( " The number is {0} " , i); Console.WriteLine(str); };
action5( 50 );
}
static void Speak( int i)
{
string str = string .Format( " The number is {0} " ,i);
Console.WriteLine(str);
}
}
输出:
The number is 10
The number is 20
The number is 30
The number is 40
The number is 50
注意:
1.Action没有返回值
2.Action非泛型委托,既没有参数也没有返回值
3.Func委托
View Code
class Program
{
static void Main( string [] args)
{
// Func委托初始化的5种方式
// 方法1
Func< int , string > func1 = new Func< int , string >(Speak);
func1( 10 );
// 方法2
Func< int , string > func2 = Speak;
func2( 20 );
// 方法3
Func< int , string > func3=( int i1)=>Speak(i1);
func3( 30 );
// 方法4
Func< int , string > func4= ( int i) => { string str = string .Format( " The number is {0} " , i); Console.WriteLine(str); return str; };
func4( 40 );
// 方法5
Func< int , string > func5 = delegate ( int i) { string str = string .Format( " The number is {0} " , i); Console.WriteLine(str); return str; };
func5( 50 );
}
static string Speak( int i)
{
string str = string .Format( " The number is {0} " ,i);
Console.WriteLine(str);
return str;
}
}
输出:
The number is 10
The number is 20
The number is 30
The number is 40
The number is 50
注:
1.Func可以无参数的,但是必须有返回值,即:无Func func;写法,只有Func<Tresult> func;写法
2.Func泛型委托,最多具有4个参数,1个返回值
.net 3.5的异步委托
1.Action的异步委托:
代码1:
View Code
class Program
{
static void Main( string [] args)
{
// Action委托的异步委托
Action< int > action = Speak;
AsyncCallback callback= new AsyncCallback(CallBackMethod);
action.BeginInvoke( 10 , callback, null );
Console.WriteLine( " 当前线程ID:{0} " , Thread.CurrentThread.ManagedThreadId.ToString());
Console.ReadKey();
}
// 回调方法
static void CallBackMethod(IAsyncResult ar)
{
AsyncResult result=(AsyncResult)ar;
Action< int > a = (Action< int >)result.AsyncDelegate;
a.EndInvoke(ar);
}
// 被委托方法
static void Speak( int i)
{
string str = string .Format( " The number is {0} " ,i);
Console.WriteLine( " 当前线程ID:{0} " ,Thread.CurrentThread.ManagedThreadId.ToString());
Thread.Sleep(TimeSpan.FromSeconds( 3 ));
Console.WriteLine(str);
}
}
注意:回调方法内部使用了IAsyncResult转换成AsyncResult类型,只是为了使用AsyncResult的属性AsyncDelegate,获取委托的对象,可以结束异步委托,同时取得返回值。
如下:
IAmBetter_Mr.w
c sharp is nothing and it is everything!
委托/事件/线程
榨干委托那些知识点,能写出多少种委托(Action Func委托+异步委托)
posted @ 2012-02-21 13:57 IAmBetter_Mr.w 阅读(1030) | 评论 (2) 编辑
异步委托,恰似一江春水向东流(你了解的异步委托)
posted @ 2012-02-13 11:32 IAmBetter_Mr.w 阅读(2082) | 评论 (11) 编辑
委托与事件 在.net的争霸战 ,你选择了谁?(异步委托产生的原因)
posted @ 2012-02-09 16:14 IAmBetter_Mr.w 阅读(1877) | 评论 (14) 编辑
抛开书本,为什么出现了事件,事件与委托有什么渊源?
posted @ 2012-02-08 15:39 IAmBetter_Mr.w 阅读(1732) | 评论 (14) 编辑
抛开书本谈 委托,为什么需要委托,它成就了什么?
posted @ 2012-02-08 11:36 IAmBetter_Mr.w 阅读(3795) | 评论 (27) 编辑
View Code
class Program
{
static void Main( string [] args)
{
// Action委托的异步委托
Action< int > action = Speak;
AsyncCallback callback= new AsyncCallback(CallBackMethod);
action.BeginInvoke( 10 , callback, action); // 注意
Console.WriteLine( " 当前线程ID:{0} " , Thread.CurrentThread.ManagedThreadId.ToString());
Console.ReadKey();
}
// 回调方法
static void CallBackMethod(IAsyncResult ar)
{
Action< int > a = (Action< int >)ar.AsyncState; // 注意
a.EndInvoke(ar);
}
// 被委托方法
static void Speak( int i)
{
string str = string .Format( " The number is {0} " ,i);
Console.WriteLine( " 当前线程ID:{0} " ,Thread.CurrentThread.ManagedThreadId.ToString());
Thread.Sleep(TimeSpan.FromSeconds( 3 ));
Console.WriteLine(str);
}
}
2.Func的异步委托
代码如下:
View Code
class Program
{
static void Main( string [] args)
{
// Action委托的异步委托
Func< int , string > func = Speak;
AsyncCallback callback= new AsyncCallback(CallBackMethod);
func.BeginInvoke( 10 , callback, func); // 注意
Console.WriteLine( " 当前线程ID:{0} " , Thread.CurrentThread.ManagedThreadId.ToString());
Console.ReadKey();
}
// 回调方法
static void CallBackMethod(IAsyncResult ar)
{
Func< int , string > a = (Func< int , string >)ar.AsyncState; // 注意
string str=a.EndInvoke(ar);
Console.WriteLine(str); // 取得返回值
}
// 被委托方法
static string Speak( int i)
{
string str = string .Format( " The number is {0} " ,i);
Console.WriteLine( " 当前线程ID:{0} " ,Thread.CurrentThread.ManagedThreadId.ToString());
Thread.Sleep(TimeSpan.FromSeconds( 3 ));
return str;
}
总结:
本文代码居多,因为有些东西抽象,不容易说明白。有什么错误望大家指出来。
3.5新特性的FCL自带的委托,如果看完本文,你就可以理解了它有什么好处。
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息查看更多关于Action Func委托+异步委托的详细内容...