异步C#方法不返回字符串

时间:2016-04-23 14:06:11

标签: c# visual-studio function asynchronous methods

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

  

异步方法的返回类型必须为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;
}

1 个答案:

答案 0 :(得分:2)

试试这个

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;
}