Delegate中的异步调用


 1 None.gif namespace  Bingosoft.Training2007.CSharp
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gif    delegate int Sum(int num1,int num2);
 4ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 5InBlock.gif    /// 使用Delegate的BeginInvoke方法完成一个函数的异步调用过程。
 6ExpandedSubBlockEnd.gif     /// </summary>

 7InBlock.gif    class Question6
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
10InBlock.gif        /// 求两个int型数的和(仅供演示)
11InBlock.gif        /// </summary>
12InBlock.gif        /// <param name="num1"></param>
13InBlock.gif        /// <param name="num2"></param>
14ExpandedSubBlockEnd.gif        /// <returns></returns>

15InBlock.gif        public static int GetSum(int num1, int num2)
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif            Thread.Sleep(1000);
18InBlock.gif            return num1 + num2;            
19ExpandedSubBlockEnd.gif        }
 
20InBlock.gif
21ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
22InBlock.gif        /// 用EndInvoke等待异步调用
23ExpandedSubBlockEnd.gif         /// </summary>

24InBlock.gif        public static void TestAsyn1()
25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
26InBlock.gif            Sum sum = new Sum(GetSum);
27InBlock.gif            IAsyncResult result = sum.BeginInvoke(1020,null,null);
28InBlock.gif            Console.WriteLine("计算中dot.gifdot.gif");
29InBlock.gif            int returnVal = sum.EndInvoke(result);
30InBlock.gif            Console.WriteLine(returnVal);
31ExpandedSubBlockEnd.gif        }
 
32InBlock.gif
33ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
34InBlock.gif        /// 用WaitHandle等待异步调用
35ExpandedSubBlockEnd.gif         /// </summary>

36InBlock.gif        public static void TestAsyn2()
37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
38InBlock.gif            Sum sum = new Sum(GetSum);
39InBlock.gif            IAsyncResult result = sum.BeginInvoke(1020nullnull);
40InBlock.gif            result.AsyncWaitHandle.WaitOne();
41InBlock.gif            Console.WriteLine("计算完毕:");
42InBlock.gif            int returnVal = sum.EndInvoke(result);
43InBlock.gif            Console.WriteLine(returnVal);
44ExpandedSubBlockEnd.gif        }
 
45InBlock.gif
46ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
47InBlock.gif        /// 轮训查询等待异步调用
48ExpandedSubBlockEnd.gif         /// </summary>

49InBlock.gif        public static void TestAsyn3()
50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
51InBlock.gif            Sum sum = new Sum(GetSum);
52InBlock.gif            IAsyncResult result = sum.BeginInvoke(1020nullnull);
53InBlock.gif            while (!result.IsCompleted)
54ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
55InBlock.gif                Console.WriteLine("计算中dot.gif");
56ExpandedSubBlockEnd.gif            }

57InBlock.gif            int returnVal = sum.EndInvoke(result);
58InBlock.gif            Console.WriteLine(returnVal);
59ExpandedSubBlockEnd.gif        }
 
60InBlock.gif
61ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
62InBlock.gif        /// 异步调用完成后,执行回调
63ExpandedSubBlockEnd.gif         /// </summary>

64InBlock.gif        public static void TestAsyn4()
65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
66InBlock.gif            Sum sum = new Sum(GetSum);
67InBlock.gif            IAsyncResult result = sum.BeginInvoke(1020new AsyncCallback(Question6.CallBackAsyn), sum);
68InBlock.gif            Console.WriteLine("计算中dot.gif");
69ExpandedSubBlockEnd.gif        }
 
70InBlock.gif
71ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
72InBlock.gif        /// 回调函数
73InBlock.gif         /// </summary>
74ExpandedSubBlockEnd.gif        /// <param name="ar"></param>

75InBlock.gif        public static void CallBackAsyn(IAsyncResult ar)
76ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
77InBlock.gif            Sum sum = (Sum)ar.AsyncState;
78InBlock.gif            int returnVal = sum.EndInvoke(ar);
79InBlock.gif            MessageBox.Show(returnVal.ToString(), "计算结果:", MessageBoxButtons.OK, MessageBoxIcon.Information);
80ExpandedSubBlockEnd.gif        }

81ExpandedSubBlockEnd.gif    }

82ExpandedBlockEnd.gif}

83 None.gif 
84 None.gif
85 None.gif // 测试Question6
86 None.gif Question6.TestAsyn1();
87 None.gifQuestion6.TestAsyn2();
88 None.gifQuestion6.TestAsyn3();
89 None.gifQuestion6.TestAsyn4();
90 None.gifConsole.WriteLine( " Press any Key to Continuedot.gif " );
91 None.gifConsole.ReadLine();
92 None.gif

转载于:https://www.cnblogs.com/happyhippy/archive/2007/07/23/827901.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值