小编Z.J*_*Z.J的帖子

在 C# 和 .NET6 中何处以及如何包含 using 指令?

我正在使用 .NET 6 在 Visual Studio Code 中对 C# 进行编程。在 .NET 6 中,不再需要显式的 main 方法。有谁知道如何以及在哪里包含 using 指令,例如 using System.Diagnostics

这就是我的程序目前的样子:

await MeakeBreakfastAsync();

async Task MakeBreakfastAsync(){
    ...
}

static async Task<string> MakeCoffeAsync() {
    ...
}

static async Task<string> MakeToastAsync() {
    ...
}
Run Code Online (Sandbox Code Playgroud)

c# omnisharp .net-6.0

0
推荐指数
1
解决办法
234
查看次数

程序在等待时崩溃?

我用 C# 编写了一个灯泡生产模拟程序。当用户在命令行界面中输入选项 1 时,异步函数开始模拟生产 10,000 个灯泡,其中 5% 的灯泡有缺陷。当检测到有缺陷的灯泡时,观察者会收到通知,并在命令提示符中显示一条消息。在新的解决方案中,我试图避免“即发即忘”的问题。相反,我将任务收集在一个列表中。可以使用选项 2 评估任务的结果。为此,必须使用await 来等待任务完成。不幸的是,当用户输入选项 2 时,程序在 main 方法中崩溃。有人能告诉我为什么吗?

Console.WriteLine("Bulb Production Monitoring!");
Main();
static async Task Main() {
    List<Task<string>> taskList = new List<Task<string>>();
    string input="";
    int maschineID=1;
    Random rndGenerator = new Random();
    Production prod=new Production();
    Console.WriteLine("1: starts a production maschine");
    Console.WriteLine("2: analyses the results");
    Console.WriteLine("3: exits the production process");
    while(!input.Equals("exit")){
        input=Console.ReadLine();
        switch(input){
            case "1": 
                prod.registerObserver(new Observer());
                taskList.Add(prod.startMaschine(maschineID, rndGenerator)); 
                maschineID++; 
                break;
            case "2":
                // while tasklist contains an element
                while (taskList.Any()){

                    // waiting for …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous async-await

-2
推荐指数
1
解决办法
158
查看次数

标签 统计

c# ×2

.net-6.0 ×1

async-await ×1

asynchronous ×1

omnisharp ×1