[英]Error with async await keywords
i added the AsyncCtpLibrary v.3. 我添加了AsyncCtpLibrary v.3。 grabbed some sample code from the async webpage. 从异步网页中获取了一些示例代码。 wrapped it in a TestFixture to play around with. 将它包裹在TestFixture中以便进行游戏。
i'm getting errors: any ideas why? 我得到错误:任何想法为什么?
Error 1 - Invalid token 'void' in class, struct, or interface member declaration 错误1 - 类,结构或接口成员声明中的标记'void'无效
Error 2 - ; 错误2 - ; expected 预期
code: 码:
[TestFixture]
public class AsyncTests
{
[Test]
public async void AsyncRunCpu()
{
Console.WriteLine("On the UI thread.");
int result = await TaskEx.Run(
() =>
{
Console.WriteLine("Starting CPU-intensive work on background thread...");
int work = DoCpuIntensiveWork();
Console.WriteLine("Done with CPU-intensive work!");
return work;
});
Console.WriteLine("Back on the UI thread. Result is {0}.", result);
}
public int DoCpuIntensiveWork()
{
// Simulate some CPU-bound work on the background thread:
Thread.Sleep(5000);
return 123;
}
}
You can't simply add the .dll to your project and have it work: the Async CTP extends the syntax of the C# language, the "normal" compiler doesn't understand the new keywords, even if the required runtime assembly is present. 您不能简单地将.dll添加到项目中并使其工作:Async CTP扩展了C#语言的语法,即使存在所需的运行时程序集,“普通”编译器也无法理解新的关键字。 You need to install it using the official installer. 您需要使用官方安装程序安装它。 (Note: uninstall all Visual Studio updates since the last service pack first, or the install won't succeed. You can reinstall the updates afterwards.) (注意:首先卸载自上一个Service Pack以来的所有Visual Studio更新,否则安装将不会成功。您可以在以后重新安装更新。)
I think it's supposed to be: 我认为它应该是:
[Test]
public async Task AsyncRunCpu()
{
// ...
}
as far as I can tell async methods with a void
return type are in some way special - wonder why the compiler doesn't issue a warning for this kind of scenario ... 据我所知,具有void
返回类型的异步方法在某种程度上是特殊的 - 想知道为什么编译器不会对这种情况发出警告......
By the way you can use async/await
with .NET4.0, just add a reference to Microsoft.BCL.Async in the project. 顺便说一句,您可以使用async/await
与.NET4.0,只需在项目中添加对Microsoft.BCL.Async的引用。 The only difference is that some Task
related functionality lives in the TaskEx
type. 唯一的区别是某些与Task
相关的功能存在于TaskEx
类型中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.