如何等待异步方法完成? [英] How to wait for async method to complete?

查看:38
本文介绍了如何等待异步方法完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个将数据传输到 USB HID 类设备的 WinForms 应用程序.我的应用程序使用了优秀的通用 HID 库 v6.0,可以在 此处 找到.简而言之,当我需要向设备写入数据时,这就是调用的代码:

I'm writing a WinForms application that transfers data to a USB HID class device. My application uses the excellent Generic HID library v6.0 which can be found here. In a nutshell, when I need to write data to the device, this is the code that gets called:

private async void RequestToSendOutputReport(List<byte[]> byteArrays)
{
    foreach (byte[] b in byteArrays)
    {
        while (condition)
        {
            // we'll typically execute this code many times until the condition is no longer met
            Task t = SendOutputReportViaInterruptTransfer();
            await t;
        }

        // read some data from device; we need to wait for this to return
        RequestToGetInputReport();
    }
}

当我的代码退出 while 循环时,我需要从设备读取一些数据.但是,设备无法立即响应,因此我需要等待此调用返回,然后才能继续.由于它目前存在,RequestToGetInputReport() 声明如下:

When my code drops out of the while loop, I need to read some data from the device. However, the device isn't able to respond right away so I need to wait for this call to return before I continue. As it currently exists, RequestToGetInputReport() is declared like this:

private async void RequestToGetInputReport()
{
    // lots of code prior to this
    int bytesRead = await GetInputReportViaInterruptTransfer();
}

就其价值而言,GetInputReportViaInterruptTransfer() 的声明如下所示:

For what it's worth, the declaration for GetInputReportViaInterruptTransfer() looks like this:

internal async Task<int> GetInputReportViaInterruptTransfer()

不幸的是,我不太熟悉 .NET 4.5 中新的异步/等待技术的工作原理.我之前读了一点关于 await 关键字的内容,这给我的印象是在 RequestToGetInputReport() 内部调用 GetInputReportViaInterruptTransfer() 会等待(也许它会等待?)但它似乎不像对 RequestToGetInputReport() 的调用本身正在等待,因为我似乎几乎立即重新进入 while 循环?

Unfortunately, I'm not very familiar with the workings of the new async/await technologies in .NET 4.5. I did a little reading earlier about the await keyword and that gave me the impression that the call to GetInputReportViaInterruptTransfer() inside of RequestToGetInputReport() would wait (and maybe it does?) but it doesn't seem like the call to RequestToGetInputReport() itself is waiting because I seem to be re-entering the while loop almost immediately?

谁能解释一下我看到的行为?

Can anyone clarify the behavior that I'm seeing?

推荐答案

避免 async void.让您的方法返回 Task 而不是 void.然后你可以await他们.

Avoid async void. Have your methods return Task instead of void. Then you can await them.

像这样:

private async Task RequestToSendOutputReport(List<byte[]> byteArrays)
{
    foreach (byte[] b in byteArrays)
    {
        while (condition)
        {
            // we'll typically execute this code many times until the condition is no longer met
            Task t = SendOutputReportViaInterruptTransfer();
            await t;
        }

        // read some data from device; we need to wait for this to return
        await RequestToGetInputReport();
    }
}

private async Task RequestToGetInputReport()
{
    // lots of code prior to this
    int bytesRead = await GetInputReportViaInterruptTransfer();
}

这篇关于如何等待异步方法完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆