A Simple Example Practice in Async Programming

本文逻辑

介绍基本概念,实现一个小的Async Programming例子, 并对一些常用的概念作一些介绍

概念介绍

查看: [Programming IL] Delegates

实例逻辑

利用Delegate - BeginInvoke的两种调用方式进行编程

  1. CallBack     回调式 
  2. Sequential    顺序执行

实例代码

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5: using System.Runtime.Remoting.Messaging;
   6:  
   7: namespace AsyncPatterns
   8: {
   9:     public class EventAsyncSample
  10:     {
  11:         private void OnCallBack(IAsyncResult result)
  12:         {
  13:             AsyncTargetCaller callback = (AsyncTargetCaller)((AsyncResult)result).AsyncDelegate;
  14:  
  15:             Int64 number = (int)result.AsyncState;
  16:             number = callback.EndInvoke(result);
  17:  
  18:             Console.WriteLine("The Result Is: {0}", number);
  19:         }
  20:  
  21:         /// <summary>
  22:         /// Call the target and invoke a method outside when done
  23:         /// </summary>
  24:  
  25:         public void AsyncCallTarget()
  26:         {
  27:             AsyncTargetCaller caller = new AsyncTargetCaller(AsyncTarget.Sum);
  28:  
  29:             AsyncCallback callback = new AsyncCallback(OnCallBack);
  30:             int number = int.MaxValue;
  31:             caller.BeginInvoke(number, callback, number);
  32:  
  33:             Console.WriteLine("Begin To Call the AsyncTarget");
  34:         }
  35:  
  36:         /// <summary>
  37:         /// No Callback, get all done in a single method
  38:         /// </summary>
  39:  
  40:         public void WaitAsyncCallTarget()
  41:         {
  42:             AsyncTargetCaller caller = new AsyncTargetCaller(AsyncTarget.Sum);
  43:             IAsyncResult result = caller.BeginInvoke(int.MaxValue, null, null);
  44:  
  45:             while (!result.IsCompleted)
  46:             {
  47:                 result.AsyncWaitHandle.WaitOne(1000);
  48:                 Console.WriteLine("Waiting for you");
  49:             }
  50:  
  51:             Console.WriteLine("WaitAsyncCallTarget Result: {0}", caller.EndInvoke(result));
  52:         }
  53:     }
  54:  
  55:     /// <summary>
  56:     /// The target to be called
  57:     /// </summary>
  58:     public class AsyncTarget
  59:     {
  60:         public static Int64 Sum(Int32 number)
  61:         {
  62:             Console.WriteLine("Entering the Sum UpdateMilestone");
  63:             Int64 m_Temp = 0;
  64:             for (int i = number; i > 0; i--)
  65:             {
  66:                 m_Temp += i;
  67:             }
  68:             return m_Temp;
  69:         }
  70:     }
  71:  
  72:     /// <summary>
  73:     /// A delegatet to call the method
  74:     /// </summary>
  75:     /// <param name="number"></param>
  76:     /// <returns></returns>
  77:     public delegate Int64 AsyncTargetCaller(int number);
  78:  
  79: }

当我们调用WaitAsyncCallTarget 输出结果:

 image

当调用AsyncCallTarget 输出结果

image 

我们一般会在在如下情况使用异步编程.

  1. 加载大容量数据
  2. 下载大量数据
  3. 网络通信和WebService调用

而这些方法都是可以使用异步编程来达到效果, 同时,在.Net Framework中一些处理也集成了处理,比如:流,WebService, WebClient等

posted on 2008-11-17 12:18 xwang 阅读(...) 评论(...) 编辑 收藏

导航

统计