【问题标题】:Async C# Method not returning a string异步 C# 方法不返回字符串
【发布时间】:2016-04-23 14:06:10
【问题描述】:

我创建了一个返回字符串值的函数,但我收到错误消息:

async 方法的返回类型必须为 void、Task 或 Task

这是代码:

private async string SerialRead()
{
    const uint maxReadLength = 1024;
    DataReader dataReader = new DataReader(SerialPort.InputStream);
    uint bytesToRead = await dataReader.LoadAsync(maxReadLength);
    string rxBuffer = dataReader.ReadString(bytesToRead);
    return rxBuffer;
}

【问题讨论】:

标签: c# visual-studio function asynchronous methods


【解决方案1】:

试试这个

private async Task<string> SerialRead()
{
    const uint maxReadLength = 1024;
    DataReader dataReader = new DataReader(SerialPort.InputStream);
    uint bytesToRead = await dataReader.LoadAsync(maxReadLength);
    string rxBuffer = dataReader.ReadString(bytesToRead);
    return rxBuffer;
}  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 2020-04-13
    • 2023-03-30
    相关资源
    最近更新 更多