AsyncResult 自訂 IAsyncResult 類

來源:互聯網
上載者:User
 代碼如下 複製代碼

 /// <summary>
    /// 非同步請求對象
    /// </summary>
    public class AsyncResult : IAsyncResult
    {
        static readonly object lockObject = new object();
        private ManualResetEvent completeEvent = null;
       
        #region IAsyncResult 成員

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="cb">回調方法</param>
        /// <param name="asyncState">使用者狀態資料</param>
        internal AsyncResult(AsyncCallback cb,object asyncState)
        {
            this.Callback = cb;
            this.AsyncState = asyncState;
            this.IsCompleted = false;
            this.ParentResult = null;
            this.Exception = null;
            this.State = null;
        }

        /// <summary>
        /// 擷取使用者狀態資料
        /// </summary>
        public object AsyncState
        {
            get;
            internal set;
        }

        /// <summary>
        /// 擷取或設定自訂狀態資料
        /// </summary>
        public object State
        {
            get;
            set;
        }

        /// <summary>
        /// 非同步等待控制代碼
        /// </summary>
        public System.Threading.WaitHandle AsyncWaitHandle
        {
            get
            {
                lock (lockObject)
                {
                    if (this.completeEvent == null)
                        this.completeEvent = new ManualResetEvent(false);

                    return this.completeEvent;
                }
            }
        }

        /// <summary>
        /// 完成同上
        /// </summary>
        public bool CompletedSynchronously
        {
            get { return false; }
        }

        /// <summary>
        /// 是否已完成
        /// </summary>
        public bool IsCompleted
        {
            get;
            private set;
        }

        #endregion


        /// <summary>
        /// 父級Result,如果存在
        /// </summary>
        public IAsyncResult ParentResult { get; set; }
       
        /// <summary>
        /// 擷取非同步完成調用
        /// </summary>
        public AsyncCallback Callback
        {
            get;
            internal set;
        }


        /// <summary>
        /// 擷取或設定非同步過程中產生的異常
        /// </summary>
        public Exception Exception { get; set; }

        /// <summary>
        /// Completes the request.
        /// </summary>
        public void Completed()
        {
            this.IsCompleted = true;
            lock (lockObject)
            {
                if (this.completeEvent != null)
                    this.completeEvent.Set();
            }

            if (this.Callback != null)
                this.Callback(this);
        }

        /// <summary>
        /// Completes the request.
        /// </summary>
        public void Completed(Exception ex) {
            this.Exception = ex;
            this.Completed();
        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.