异步方法

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;

using  Common;

namespace  web
{
    
//  异步发送邮件委托
     public   delegate   void  DelegateMail();

    
///   <summary>
    
///  异步类
    
///   </summary>
     public   class  Async
    {
        
#region  异步发送邮件

        
public   static   void  SendMail(CMail mail)
        {
            
//  实例化委托并初赋值
            DelegateMail dm  =   new  DelegateMail(mail.Send);
            
//  实例化回调方法
            
//  把AsyncCallback看成Delegate你就懂了,实际上AsyncCallback是一种特殊的Delegate,就像Event似的
            AsyncCallback acb  =   new  AsyncCallback(MailCallBack);
            dm.BeginInvoke(acb, dm);
        }

        
public   static   void  MailCallBack(IAsyncResult result)
        {
            
//  从异步状态ar.AsyncState中,获取委托对象
            DelegateMail dm  =  (DelegateMail)result.AsyncState;
            
//  一定要EndInvoke
            dm.EndInvoke(result);
        }

        
#endregion
    }

    

}

你可能感兴趣的:(方法)