Visual Studio上的我的控制台应用程序正在自动关闭,所以我想使用像C这样的东西system("PAUSE")在执行结束时"暂停"应用程序,我该如何实现?
我有这个简单的代码:
public static async Task<int> SumTwoOperationsAsync()
{
    var firstTask = GetOperationOneAsync();
    var secondTask = GetOperationTwoAsync();
    return await firstTask + await secondTask;
}
private async Task<int> GetOperationOneAsync()
{
    await Task.Delay(500); // Just to simulate an operation taking time
    return 10;
}
private async Task<int> GetOperationTwoAsync()
{
    await Task.Delay(100); // Just to simulate an operation taking time
    return 5;
}
大.这个编译.
但是让我们说我有一个控制台应用程序,我想运行上面的代码(调用SumTwoOperationsAsync())
 static  void Main(string[] args)
        {
             SumTwoOperationsAsync();
        }
但我读过,(使用时sync),我不得不一路同步向上和向下   :
问题:这是否意味着我的Main功能应该标记为 …
我在 .Net Core 3.1 中创建了一个简单的控制台应用程序。我想创建一个记录器并将其打印到控制台。
我有以下代码
class Program
{
    public static void Main(string[] args)
    {
        var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
        var logger = loggerFactory.CreateLogger(nameof(Program));
        logger.LogInformation("some logging info");
    }
}
当我运行命令dotnet run时,没有打印任何内容。
我还需要配置其他东西吗?
假设我有一个C#WinForms应用程序,它只是通过使用外部程序启动Process.Start(MyModule.exe).
我试图通过使用我的项目属性调试我的代码- > 调试 - > 启动操作 - > 启动外部程序(当然设置正确的工作目录).
我的另一个尝试是Debug - > Attach to process
更新:我的模块和外部程序都使用一个资源.所以外部程序释放它,然后调用Process.Start(),等待我的进程退出ans然后再次捕获资源.所以当你的程序已经运行时我无法附加.
它们都失败了 - Program.cs中的断点从未被击中.
那么,在这种情况下如何调试我的代码呢?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
    static void Main(string[] args)
    {
        string userName = "";
        int userAge = 0;
        int currentYear = 0;
        Console.Write("Please enter your name: ");
            userName = Console.ReadLine();
        Console.Write("Please enter your age: ");
        userAge = Convert.ToInt32(Console.ReadLine());
        Console.Write("Please enter the current year: ");
        currentYear = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Hello World, my name is {0} and I'm {1} years old, I was born on the year {2} .", userName, userAge, currentYear …我想阻止CMD在运行程序后自动关闭.
码:
static void Main(string[] args)
    {
        FileStream textfsw = new FileStream("fileone.txt", FileMode.Append, FileAccess.Write); // CREATING FILE
        StreamWriter textsw = new StreamWriter(textfsw);
        Console.Write(" Enter your ID: ");
        int ID = int.Parse(Console.ReadLine());
        Console.Write(" Enter your Name: ");
        string Name = Console.ReadLine();
        Console.WriteLine(" Thank you! your logged in as" +Name+ +ID);
        textsw.Close();
        textfsw.Close();
        FileStream textfs = new FileStream("fileone.txt", FileMode.Open, FileAccess.Read); // READING A FILE USING ReadAllLines
        StreamReader textsr = new StreamReader(textfs);
        String[] lines = File.ReadAllLines("fileone.txt");
    }
截图:

我希望在运行后显示以下内容并阻止cmd关闭:
Thank you! your logged …Soooo这个程序似乎是合乎逻辑的,而且我已经能够在c ++和java中创建具有相同想法的程序,每当我运行它时,一切都会突然关闭......也许我只是下载了错误的东西
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            /* 
            int[] numbahs= new int[5];
            numbahs[0] = 4;
            numbahs[1] = 3;
            Console.WriteLine(numbahs[1].ToString());
            string[] cow = new string[] { "pee", "poop" };
            foreach (string pee in cow)
            {
                Console.WriteLine(pee);
            }
            */
            int x;
            int y;
            Console.WriteLine("enter x:");
            x = Console.Read();
            Console.WriteLine("enter y:");
            y = Console.Read();
            int z = (x > y)? 8 : 2;
            Console.WriteLine("x:{0} y:{1} z:{2}",x ,y, …c# ×7
.net-4.5 ×1
.net-core ×1
async-await ×1
c#-5.0 ×1
console ×1
debugging ×1
filestream ×1
logging ×1