利用异步委托实现异步编程

ContractedBlock.gif ExpandedBlockStart.gif 异步方法及其委托
 1None.gifusing System;
 2None.gifusing System.Threading; 
 3None.gif
 4None.gifnamespace Examples.AdvancedProgramming.AsynchronousOperations
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6InBlock.gif    public class AsyncDemo 
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        // The method to be executed asynchronously.
 9InBlock.gif        public string TestMethod(int callDuration, out int threadId) 
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            Console.WriteLine("Test method begins.");
12InBlock.gif            Thread.Sleep(callDuration);
13InBlock.gif            threadId = Thread.CurrentThread.GetHashCode();
14InBlock.gif            return String.Format("My call time was {0}.", callDuration.ToString());
15ExpandedSubBlockEnd.gif        }

16ExpandedSubBlockEnd.gif    }

17InBlock.gif    // The delegate must have the same signature as the method
18InBlock.gif    // it will call asynchronously.
19InBlock.gif    public delegate string AsyncMethodCaller(int callDuration, out int threadId);
20ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif 主调用方法
 1None.gifusing System;
 2None.gifusing System.Threading;
 3None.gif
 4None.gifnamespace Examples.AdvancedProgramming.AsynchronousOperations
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6InBlock.gif    public class AsyncMain 
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        static int threadId;
 9InBlock.gif
10InBlock.gif        public static void Main() 
11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
12InBlock.gif            // The asynchronous method puts the thread id here.
13InBlock.gif
14InBlock.gif            // Create an instance of the test class.
15InBlock.gif            AsyncDemo ad = new AsyncDemo();
16InBlock.gif
17InBlock.gif            // Create the delegate.
18InBlock.gif            AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);
19InBlock.gif       
20InBlock.gif//            // Initiate the asychronous call.
21InBlock.gif//            IAsyncResult result = caller.BeginInvoke(3000,out threadId, null, null);
22InBlock.gif
23InBlock.gif            IAsyncResult result = caller.BeginInvoke(3000,
24InBlock.gif                out threadId, 
25InBlock.gif                new AsyncCallback(CallbackMethod),
26InBlock.gif                caller );
27InBlock.gif
28InBlock.gif            Console.WriteLine("Press Enter to close application.");
29InBlock.gif            Console.ReadLine();
30InBlock.gif
31InBlock.gif
32InBlock.gif//            Thread.Sleep(0);
33InBlock.gif//            Console.WriteLine("Main thread {0} does some work.",
34InBlock.gif//                Thread.CurrentThread.GetHashCode());
35InBlock.gif//
36InBlock.gif//            // Wait for the WaitHandle to become signaled.
37InBlock.gif//            result.AsyncWaitHandle.WaitOne();
38InBlock.gif//
39InBlock.gif//            // Poll while simulating work.
40InBlock.gif//            while(result.IsCompleted == false) 
41InBlock.gif//            {
42InBlock.gif//                Thread.Sleep(10);
43InBlock.gif//            }
44InBlock.gif//
45InBlock.gif//            // Call EndInvoke to wait for the asynchronous call to complete,
46InBlock.gif//            // and to retrieve the results.
47InBlock.gif//            string returnValue = caller.EndInvoke(out threadId, result);
48InBlock.gif//
49InBlock.gif//            Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
50InBlock.gif//                threadId, returnValue);
51InBlock.gif//            Console.ReadLine();
52ExpandedSubBlockEnd.gif        }

53InBlock.gif        static void CallbackMethod(IAsyncResult ar) 
54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
55InBlock.gif            // Retrieve the delegate.
56InBlock.gif            AsyncMethodCaller caller = (AsyncMethodCaller) ar.AsyncState;
57InBlock.gif
58InBlock.gif            // Call EndInvoke to retrieve the results.
59InBlock.gif            string returnValue = caller.EndInvoke(out threadId, ar);
60InBlock.gif
61ExpandedSubBlockStart.gifContractedSubBlock.gif            Console.WriteLine("The call executed on thread {0}, with return value \"dot.gif{1}\".",
62InBlock.gif                threadId, returnValue);
63ExpandedSubBlockEnd.gif        }

64ExpandedSubBlockEnd.gif    }

65ExpandedBlockEnd.gif}
当主调用线程,调用异步方法后,可以阻塞自己,也可以处理一些操作。也可以自定义异步线程的回调方法。还在学习中。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值