异步超时 UWP
Async Timeout UWP
参考线程,接受的答案Asynchronously wait for Task to complete with timeout
我的方法UART_receive() returns一个字符串。如何从超时任务中取出字符串?
现在显然行不通。没有超时任务,我的串行通信工作正常。
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;
}
How do I get the string out of the timeout task?
string rxBuffer = await task1
参考线程,接受的答案Asynchronously wait for Task to complete with timeout
我的方法UART_receive() returns一个字符串。如何从超时任务中取出字符串?
现在显然行不通。没有超时任务,我的串行通信工作正常。
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;
}
How do I get the string out of the timeout task?
string rxBuffer = await task1