非同步思考-IAsyncResult介面

來源:互聯網
上載者:User
上面的例子可以看出,IAsyncResult 這個介面是關鍵。 我查閱了很多資料,發現提供這個介面的大部分是IO等操作。而且介面內部機制被.net封裝了,要反編譯才能看見。 這樣,實際上asp.net的非同步作業被asp.net限制住在幾個方面了(Stream/Web Service ...)  Reference:Using asynchronous method calls in C#http://www.builderau.com.au/program/java/soa/Using_asynchronous_method_calls_in_C_/0,339024620,320264369,00.htm   調用過程:   

 void Page_Load (object sender, EventArgs e)
    {
        AddOnPreRenderCompleteAsync (
            new BeginEventHandler(BeginAsyncOperation),
            new EndEventHandler (EndAsyncOperation)
        );
    }
    IAsyncResult BeginAsyncOperation (object sender, EventArgs e,
        AsyncCallback cb, object state)
    {
      //省略
    }
    void EndAsyncOperation (IAsyncResult ar)
    {
      //省略
    }

 IAsyncResult 介面描述:    // 摘要:
    //     表示非同步作業的狀態。
    [ComVisible(true)]
    public interface IAsyncResult
    {
        // 摘要:
        //     擷取使用者定義物件,它限定或包含關於非同步作業的資訊。
        //
        // 返回結果:
        //     使用者定義物件,它限定或包含關於非同步作業的資訊。
        object AsyncState { get; }
        //
        // 摘要:
        //     擷取用於等待非同步作業完成的 System.Threading.WaitHandle。
        //
        // 返回結果:
        //     用於等待非同步作業完成的 System.Threading.WaitHandle。
        WaitHandle AsyncWaitHandle { get; }
        //
        // 摘要:
        //     擷取非同步作業是否同步完成的指示。
        //
        // 返回結果:
        //     如果非同步作業同步完成,則為 true;否則為 false。
        bool CompletedSynchronously { get; }
        //
        // 摘要:
        //     擷取非同步作業是否已完成的指示。
        //
        // 返回結果:
        //     如果操作完成則為 true,否則為 false。
        bool IsCompleted { get; }
    }
 上面是一個介面的描述。。AsyncState返回的值與調用過程中BeginAsyncOperation (object sender, EventArgs e, AsyncCallback cb, object state)的state一樣。。AsyncWaitHandle:就是多個線程操作時候控制用的。例如非同步中我開了n個線程平行操作,我需要所有線程完成的時候才返回,就使用這個。。CompletedSynchronously表示我的操作是否實際上已經同步完成了(True)。當然如果是非同步就false。IsCompleted 表示非同步作業是否完成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.