stream.ReadAsync()
我渴望在我的这篇文章中添加超时功能并阅读 Microsoft 帮助Async Cancellation: Bridging between the .NET Framework and the Windows Runtime (C# and Visual Basic)。本文建议使用AsTask()
来实现这一点。但是我的 C# 似乎根本无法识别AsTask()
。
我正在使用带有Windows 7的Visual Studio 2013 ,这是我拥有的一段代码:using System.Threading;
private async Task<int> ReadAsync(BluetoothClient Client)
{
const int timeoutMs = 10000;
var cancelationToken = new CancellationTokenSource(timeoutMs);
var stream = Client.GetStream();
byte[] buffer = { 0 };
int offset = 0;
int count = 1;
StatusManager("~Async Read Started...");
//This line works perfectly fine.
var rx = await stream.ReadAsync(buffer, offset, count);
//This line gets underlined with error for AsTask()
var rx = await stream.ReadAsync(buffer, offset, count).AsTask(cancelationToken);
//rx.AsTask().Start();
StatusManager("Recieved byte " + rx.ToString());
StatusManager("~Async Read Finished.");
}
伙计们,我在这里想念什么。我很困惑:)
更新:这些是安装的 .NET 包列表,我会说 Visual Studio 2013 使用 4.5