相关疑难解决方法(0)

如何等待异步方法完成?

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

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

对于它的价值,GetInputReportViaInterruptTransfer()的声明如下所示:

internal …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous async-await

129
推荐指数
4
解决办法
26万
查看次数

标签 统计

async-await ×1

asynchronous ×1

c# ×1