【问题标题】:Async Timeout UWP异步超时 UWP
【发布时间】:2019-10-19 17:02:30
【问题描述】:

参考线程,接受答案Asynchronously wait for Task to complete with timeout

我的方法 UART_receive() 返回一个字符串。如何从超时任务中取出字符串?
现在显然行不通。如果没有超时任务,我的串行通信工作正常。

using System;
using Windows.Storage.Streams;              //Uart Seriell
using Windows.Devices.Enumeration;          //UART Seriell
using Windows.Devices.SerialCommunication;  //UART Seriell

public MainPage()
{
    this.InitializeComponent();

    Serial();
}

public async void Serial()
{
    //configure serial setting
    //configure serial setting

    while (true)
    {
        UART_send("+");

        //receive
        int timeout = 1000;
        var task1 = UART_receive();

        if (await Task.WhenAny(task1, Task.Delay(timeout)) == task1)
        {
            statusbar_main.Text = "ok";
        }
        else
        {
            statusbar_main.Text = "not ok";
        }
        string rxBuffer = Convert.ToString(task1);
    }
}

private async Task<string> UART_receive()
{
    const uint maxReadLength = 13;

    uint bytesToRead = await dataReader.LoadAsync(maxReadLength);
    string rxBuffer = dataReader.ReadString(bytesToRead);

    return rxBuffer;
}

【问题讨论】:

    标签: c# uwp timeout uart


    【解决方案1】:

    如何从超时任务中取出字符串?

    字符串 rxBuffer = 等待任务 1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-20
      • 2016-09-04
      • 1970-01-01
      • 2011-06-15
      • 2018-05-21
      相关资源
      最近更新 更多