C#基础系列问题三委托知识总结2
Action<T>:封装一个方法,该方法只采用一个参数并且不返回值,包括
Action<T>,Action<T1,T2>,Action<T1,T2,T3>,Action<T1,T2,T3,T4>
这几种情况,也可以
通过扩展方法去扩展参数的个数 。
Action 委托使用例子
1 public void TestAction()
2 {
3 TEventSource eventSource = new TEventSource();
4 Action<string> action = eventSource.Speak;
5 action("Action<T> 泛型委托");
6 }
7
8 public void Speak(string context)
9 {
10 Console.WriteLine(context);
11 }
2 {
3 TEventSource eventSource = new TEventSource();
4 Action<string> action = eventSource.Speak;
5 action("Action<T> 泛型委托");
6 }
7
8 public void Speak(string context)
9 {
10 Console.WriteLine(context);
11 }
Predicate<T>:表示定义一组条件并确定指定对象是否符合这些条件的方法。该委托返回的是一个bool类型的值,如果比较满足条件
返回true,否则返回false.其实上面的
Func 委托可以包含这个委托.不过这个委托和上面的两个不一样,它只有一种类型
Predicate 委托使用例子
1 public void TestPredicate()
2 {
3 TEventSource eventSource = new TEventSource();
4 Predicate<int> predicate = eventSource.IsRigth;
5 Console.WriteLine(predicate(0));
6 }
7
8 public bool IsRigth(int value)
9 {
10 if (value == 0)
11 {
12 return true;
13 }
14 else
15 {
16 return false;
17 }
18 }
2 {
3 TEventSource eventSource = new TEventSource();
4 Predicate<int> predicate = eventSource.IsRigth;
5 Console.WriteLine(predicate(0));
6 }
7
8 public bool IsRigth(int value)
9 {
10 if (value == 0)
11 {
12 return true;
13 }
14 else
15 {
16 return false;
17 }
18 }
6. 异步委托
投票技术: 委托其实相当于一个线程,使用投票技术是使用异步委托的一种实现方式.Delegate类提供了方法BeginInvoke(),可以传送委托类型定义的 输入参数,其返回类型为IAsyncResult。IAsyncResult的IsCompleted属性可以判断委托任务是否完成
异步委托投票技术
1 public delegate int DelegateVote(int data, int ms);
2 /// <summary>
3 /// 使用投票操作完成委托任务
4 /// </summary>
5 public class VoteDelegate
6 {
7 /// <summary>
8 /// 休眠特定时间执行操作
9 /// </summary>
10 /// <param name="data"></param>
11 /// <param name="ms"></param>
12 /// <returns></returns>
13 public static int TakeWork(int data, int ms)
14 {
15 Console.WriteLine("开始调用TakeWork方法");
16 Thread.Sleep(ms);
17 Console.WriteLine("结束调用 TakeWork方法");
18 return data + 10;
19 }
20
21 public void TestDelegate()
22 {
23 DelegateVote voteDel = TakeWork;
24 IAsyncResult result = voteDel.BeginInvoke(1,5000,null,null);
25 while (result.IsCompleted == false)
26 {
27 Console.WriteLine("等待......");
28 Thread.Sleep(500);
29 }
30 int value = voteDel.EndInvoke(result);
31 Console.WriteLine("委托调用结果: "+value);
32 }
33 }
2 /// <summary>
3 /// 使用投票操作完成委托任务
4 /// </summary>
5 public class VoteDelegate
6 {
7 /// <summary>
8 /// 休眠特定时间执行操作
9 /// </summary>
10 /// <param name="data"></param>
11 /// <param name="ms"></param>
12 /// <returns></returns>
13 public static int TakeWork(int data, int ms)
14 {
15 Console.WriteLine("开始调用TakeWork方法");
16 Thread.Sleep(ms);
17 Console.WriteLine("结束调用 TakeWork方法");
18 return data + 10;
19 }
20
21 public void TestDelegate()
22 {
23 DelegateVote voteDel = TakeWork;
24 IAsyncResult result = voteDel.BeginInvoke(1,5000,null,null);
25 while (result.IsCompleted == false)
26 {
27 Console.WriteLine("等待......");
28 Thread.Sleep(500);
29 }
30 int value = voteDel.EndInvoke(result);
31 Console.WriteLine("委托调用结果: "+value);
32 }
33 }
等待句柄:等待句柄是使用AsyncWaitHandle属性访问,返回一个WaitHandle类型的对象,它可以等待委托线程完成其任务。在这 个参数中可以设置最大的等待时间。
异步委托等待句柄
1 public delegate string WaitDelegate(string content);
2
3 public class WaitHandlerDelegate
4 {
5 public void TestWaitHander()
6 {
7 WaitDelegate del = GetTea;
8 IAsyncResult ar = del.BeginInvoke("hechen", null, null);
9 while (true)
10 {
11 Console.Write(".");
12 if (ar.AsyncWaitHandle.WaitOne(50, false))
13 {
14 break;
15 }
16 }
17 string result=del.EndInvoke(ar);
18 Console.WriteLine(result);
19
20 }
21
22 public static string GetTea(string content)
23 {
24 return "茶来了 "+content;
25 }
26 }
2
3 public class WaitHandlerDelegate
4 {
5 public void TestWaitHander()
6 {
7 WaitDelegate del = GetTea;
8 IAsyncResult ar = del.BeginInvoke("hechen", null, null);
9 while (true)
10 {
11 Console.Write(".");
12 if (ar.AsyncWaitHandle.WaitOne(50, false))
13 {
14 break;
15 }
16 }
17 string result=del.EndInvoke(ar);
18 Console.WriteLine(result);
19
20 }
21
22 public static string GetTea(string content)
23 {
24 return "茶来了 "+content;
25 }
26 }
异步回调:这个方式和投票技术有点类似,不过在投票方式中BeginInvoke()方法第三个参数指定了一个方法签名,而这个方法参数接收 IAsyncResult 类型的参数。
异步委托回调函数
1 public delegate string AsyDelegate(string content);
2
3 public class AsyncresultDelegate
4 {
5 public void TestAsync()
6 {
7 AsyDelegate del = GetTea;
8 del.BeginInvoke("hechen", delegate(IAsyncResult ar) {
9 Thread.Sleep(5000);
10 string result = del.EndInvoke(ar);
11 Console.WriteLine(result);
12 }, null);
13 for (int i = 0; i < 100; i++)
14 {
15 Console.WriteLine("等待.....");
16 Thread.Sleep(1000);
17 }
18 }
19
20 public static string GetTea(string content)
21 {
22 return "茶来了 " + content;
23 }
24 }
2
3 public class AsyncresultDelegate
4 {
5 public void TestAsync()
6 {
7 AsyDelegate del = GetTea;
8 del.BeginInvoke("hechen", delegate(IAsyncResult ar) {
9 Thread.Sleep(5000);
10 string result = del.EndInvoke(ar);
11 Console.WriteLine(result);
12 }, null);
13 for (int i = 0; i < 100; i++)
14 {
15 Console.WriteLine("等待.....");
16 Thread.Sleep(1000);
17 }
18 }
19
20 public static string GetTea(string content)
21 {
22 return "茶来了 " + content;
23 }
24 }