zoukankan      html  css  js  c++  java
  • C#异步编程模式IAsyncResult概述

    C#异步编程模式IAsyncResult概述

        IAsyncResult 异步设计模式通过名为 BeginOperationName 和 EndOperationName 的两个方法来实现原同步方法的异步调用,如 FileStream 类提供了 BeginRead 和 EndRead 方法来从文件异步读取字节,它们是 Read 方法的异步版本

        Begin 方法包含同步方法签名中的任何参数,此外还包含另外两个参数:一个AsyncCallback 委托和一个用户定义的状态对象。委托用来调用回调方法,状态对象是用来向回调方法传递状态信息。该方法返回一个实现 IAsyncResult 接口的对象

        End 方法用于结束异步操作并返回结果,因此包含同步方法签名中的 ref 和 out 参数,返回值类型也与同步方法相同。该方法还包括一个 IAsyncResult 参数,用于获取异步操作是否完成的信息,当然在使用时就必须传入对应的 Begin 方法返回的对象实例

        开始异步操作后如果要阻止应用程序,可以直接调用 End 方法,这会阻止应用程序直到异步操作完成后再继续执行。也可以使用 IAsyncResult 的 AsyncWaitHandle 属性,调用其中的WaitOne等方法来阻塞线程。这两种方法的区别不大,只是前者必须一直等待而后者可以设置等待超时

        如果不阻止应用程序,则可以通过轮循 IAsyncResult 的 IsCompleted 状态来判断操作是否完成,或使用 AsyncCallback 委托来结束异步操作。AsyncCallback 委托包含一个 IAsyncResult 的签名,回调方法内部再调用 End 方法来获取操作执行结果

    代码
    C#异步编程模式IAsyncResult之IAsyncResult 接口

    public interface IAsyncResult

    {

    object AsyncState { get; }

    WaitHandle AsyncWaitHandle {
    get; }

    bool CompletedSynchronously { get; }

    bool IsCompleted { get; }

    }

    我用一个 AsyncDemo 类作为异步方法的提供者,后面的程序都会调用它。内部很简单,构造函数接收一个字符串作为 name ,Run 方法输出
    "My name is " + name ,而异步方法直接用委托的 BeginInvoke 和 EndInvoke 方法实现

    public class AsyncDemo

    {

    // Use in asynchronous methods

    private delegate string runDelegate();



    private string m_Name;

    private runDelegate m_Delegate;



    public AsyncDemo(string name)

    {

    m_Name
    = name;

    m_Delegate
    = new runDelegate(Run);

    }



    /**//// ﹤summary﹥

    /// Synchronous method

    /// ﹤/summary﹥

    /// ﹤returns﹥﹤/returns﹥

    public string Run()

    {

    return "My name is " + m_Name;

    }



    /**//// ﹤summary﹥

    /// Asynchronous begin method

    /// ﹤/summary﹥

    /// ﹤param name="callBack"﹥﹤/param﹥

    /// ﹤param name="stateObject"﹥﹤/param﹥

    /// ﹤returns﹥﹤/returns﹥

    public IAsyncResult BeginRun(

    AsyncCallback callBack, Object stateObject)

    {

    try

    {

    return m_Delegate.BeginInvoke(callBack, stateObject);

    }

    catch(Exception e)

    {

    // Hide inside method invoking stack

    throw e;

    }

    }



    /**//// ﹤summary﹥

    /// Asynchronous end method

    /// ﹤/summary﹥

    /// ﹤param name="ar"﹥﹤/param﹥

    /// ﹤returns﹥﹤/returns﹥

    public string EndRun(IAsyncResult ar)

    {

    if (ar == null)

    throw new NullReferenceException(

    "Arggument ar can't be null");



    try

    {

    return m_Delegate.EndInvoke(ar);

    }

    catch (Exception e)

    {

    // Hide inside method invoking stack

    throw e;

    }

    }

    }

    C#异步编程模式IAsyncResult操作步骤:首先是 Begin 之后直接调用 End 方法,当然中间也可以做其他的操作

    class AsyncTest

    {

    static void Main(string[] args)

    {

    AsyncDemo demo
    = new AsyncDemo("jiangnii");



    // Execute begin method

    IAsyncResult ar
    = demo.BeginRun(null, null);



    // You can do other things here



    // Use end method to block thread

    // until the operation is complete

    string demoName = demo.EndRun(ar);

    Console.WriteLine(demoName);

    }

    }

    也可以用 IAsyncResult 的 AsyncWaitHandle 属性,我在这里设置为1秒超时

    class AsyncTest

    {

    static void Main(string[] args)

    {

    AsyncDemo demo
    = new AsyncDemo("jiangnii");



    // Execute begin method

    IAsyncResult ar
    = demo.BeginRun(null, null);



    // You can do other things here



    // Use AsyncWaitHandle.WaitOne method to block thread for 1 second at most

    ar.AsyncWaitHandle.WaitOne(
    1000, false);



    if (ar.IsCompleted)

    {

    // Still need use end method to get result,

    // but this time it will return immediately

    string demoName = demo.EndRun(ar);

    Console.WriteLine(demoName);

    }

    else

    {

    Console.WriteLine(
    "Sorry,

    can
    't get demoName, the time is over");

    }

    }

    }

    C#异步编程模式IAsyncResult要注意的还有:不中断的循环,每次循环输出一个
    "."

    class AsyncTest

    {

    static void Main(string[] args)

    {

    AsyncDemo demo
    = new AsyncDemo("jiangnii");



    // Execute begin method

    IAsyncResult ar
    = demo.BeginRun(null, null);



    Console.Write(
    "Waiting..");

    while (!ar.IsCompleted)

    {

    Console.Write(
    ".");

    // You can do other things here

    }

    Console.WriteLine();



    // Still need use end method to get result,

    //but this time it will return immediately

    string demoName = demo.EndRun(ar);

    Console.WriteLine(demoName);

    }

    }

    最后是使用回调方法并加上状态对象,状态对象被作为 IAsyncResult 参数的 AsyncState 属性被传给回调方法。回调方法执行前不能让主线程退出,我这里只是简单的让其休眠了1秒。另一个与之前不同的地方是 AsyncDemo 对象被定义成了类的静态字段,以便回调方法使用

    class AsyncTest

    {

    static AsyncDemo demo = new AsyncDemo("jiangnii");



    static void Main(string[] args)

    {

    // State object

    bool state = false;



    // Execute begin method

    IAsyncResult ar
    = demo.BeginRun(

    new AsyncCallback(outPut), state);



    // You can do other thins here



    // Wait until callback finished

    System.Threading.Thread.Sleep(
    1000);

    }



    // Callback method

    static void outPut(IAsyncResult ar)

    {

    bool state = (bool)ar.AsyncState;

    string demoName = demo.EndRun(ar);



    if (state)

    {

    Console.WriteLine(demoName);

    }

    else

    {

    Console.WriteLine(demoName
    + ", isn't it?");

    }

    }

    }

       
        C#异步编程模式IAsyncResult的后话:

        对于一个已经实现了 BeginOperationName 和 EndOperationName方法的对象,我们可以直接用上述方式调用,但对于只有同步方法的对象,我们要对其进行异步调用也不需要增加对应的异步方法,而只需定义一个委托并使用其 BeginInvoke 和 EndInvoke 方法就可以了。

        C#异步编程模式IAsyncResult的基本情况就向你介绍到这里,希望对你了解和学习C#异步编程模式IAsyncResult有所帮助。

    作者:jiangnii
    原文链接:http://www.csharpwin.com/csharpspace/6129r6795.shtml 



    返回导读目录,阅读更多随笔



    分割线,以下为博客签名:

    软件臭虫情未了
    • 编码一分钟
    • 测试十年功


    随笔如有错误或不恰当之处、为希望不误导他人,望大侠们给予批评指正。

  • 相关阅读:
    5. Redis持久化
    4.Redis客户端
    3.Redis高级功能
    2.Redis五种数据结构
    1.Redis简介
    32.Mysql Cluster
    suffix ACM-ICPC 2017 Asia Qingdao
    多层BFS
    我想和你们说说java和C++___C加加
    11073 最热门的K个搜索串
  • 原文地址:https://www.cnblogs.com/08shiyan/p/1875397.html
Copyright © 2011-2022 走看看