Skip to main content
 首页 » 编程设计

c#之async await 关键字出错

2024年02月23日25lovecherry

我添加了 AsyncCtpLibrary v.3。从异步网页中获取了一些示例代码。将其包装在一个 TestFixture 中以供使用。

我遇到错误:有什么想法吗?

错误 1 ​​- 类、结构或接口(interface)成员声明中的无效标记“void”

错误 2 - ;预计

代码:

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

请您参考如下方法:

您不能简单地将 .dll 添加到您的项目并让它运行:Async CTP 扩展了 C# 语言的语法,“普通”编译器不理解新关键字,即使所需的运行时程序集存在。您需要使用官方安装程序安装它。 (注意:首先卸载自上次服务包以来的所有 Visual Studio 更新,否则安装将不会成功。您可以在之后重新安装更新。)