异步编程详解

异步调用的函数介绍及解释

  1. AsyncCallback(IAsyncResult re) - - 回调函数(委托类型)
    他的参数为异步结果这个异步结果可以为任何函数
  2. IAsyncResult - - 接口类型函数
    可以实现一些函数的赋值
    例如:现在有一个委托
public delegate double AddDelegate(int nu);//取nu是更好的辨认

当我们要给IAsyncResult赋值并且把他放入回调函数中
3. 可以使用BeginInvoke函数,他的返回值是IAsyncResult 类型
并且他也是启动异步回调的函数

我们可以写成

IAsyncResult result = addDel.BeginInvoke(20, callBack, 10);

其中他的参数列表如下
在这里插入图片描述

  • nu就是我们之前写的自定义函数AddDelegate中的参数为nu,如果我们这个自定义委托中有两个参数,那么就会是
    在这里插入图片描述
    前面会再加一个参数,相当于前面那一部分会是我们自定义委托的参数。
  • 倒数第二个参数就是我们的回调函数,上图中的callBack是我们实例化的一个委托
    在这里插入图片描述
    在这里插入图片描述
    这个回调函数的委托中的参数为 IAsyncResult 类型
  • 最后一个参数object是IAsyncResult 类型中的AsyncState
    其中IAsyncResult接口内部为

    public interface IAsyncResult
    {
        //
        // 摘要:
        //     Gets a user-defined object that qualifies or contains information about an asynchronous
        //     operation.
        //
        // 返回结果:
        //     A user-defined object that qualifies or contains information about an asynchronous
        //     operation.
        object? AsyncState { get; }  //这个就是我们的object参数赋的值
        //
        // 摘要:
        //     Gets a System.Threading.WaitHandle that is used to wait for an asynchronous operation
        //     to complete.
        //
        // 返回结果:
        //     A System.Threading.WaitHandle that is used to wait for an asynchronous operation
        //     to complete.
        WaitHandle AsyncWaitHandle { get; }
        //
        // 摘要:
        //     Gets a value that indicates whether the asynchronous operation completed synchronously.
        //
        // 返回结果:
        //     true if the asynchronous operation completed synchronously; otherwise, false.
        bool CompletedSynchronously { get; }
        //
        // 摘要:
        //     Gets a value that indicates whether the asynchronous operation has completed.
        //
        // 返回结果:
        //     true if the operation is complete; otherwise, false.
        bool IsCompleted { get; }
    }
  • EndInvoke(IAsyncResult re) 函数
    他的返回的是re的函数的返回结果,当然他的主要作用的用来阻塞异步调用,
    如果我们的re函数为
    在这里插入图片描述
    那么这个函数的返回类型就是double且结果为之前在BeginInvoke函数中参数传递后的结果。

异步编程

  1. 函数介绍 BeginAccept(AsyncCallback asyncCallback,Ojbectstate)
    在这里插入图片描述
    跟BeginInvoke很像,都是一样的。只是这个是在异步编程传输数据的时候使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我已经怒不可遏了!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值