简体   繁体   English

在C#中经过X秒钟后使cmd吗?

[英]Make cmd after an X amount of seconds in C#?

I'm trying to make the console window close after an X amount of seconds here's an example: 我试图在X秒钟后关闭控制台窗口,这是一个示例:

My program says "Type in a number:" 我的程序说“输入数字:”

Then I type "1945" 然后我输入“ 1945”

After I type the number the console says "Unlocked". 输入数字后,控制台会显示“ Unlocked”。

The problem is that I want the console to re-run the original message after 3-4 seconds so that it just continuously will do this in a loop. 问题是我希望控制台在3-4秒后重新运行原始消息,以使其连续不断地循环执行。

Down below I have included the current code with comments on where I want it do do my wanted things. 在下面,我将当前代码包含在内,并在注释中说明了我希望它在哪里做我想要的事情。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace portkod
{
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Kod:");

        int kod = Convert.ToInt32(Console.ReadLine());
        DateTime tiden = DateTime.Now;



        if (kod == 1946)
        {
            Console.Clear();

            //After this message comes up I want the program to ask for "Kod:"
            //again after like 3-4 seconds.

            Console.WriteLine("Unlocked!");
        }
        else if (kod != 1946)
        {
            Console.Clear();

            Console.WriteLine("Locked!");
        }
        Console.ReadLine();
    }
}
}

You could do it like this. 您可以这样做。

The GetIntFromConsole reads from the console, and only returns a nr when a valid integer was given, if not asks at once again. GetIntFromConsole从控制台读取,并且仅在给出有效整数时才返回nr,如果没有再次询问则返回nr。

As long as the user doesn't give in 1946, the program repeats the question after 4 seconds, and marks 'Locked' 只要用户在1946年没有给出提示,程序就会在4秒钟后重复该问题,并标记为“已锁定”

using System;
using System.Threading;

namespace Repeat
{
    class Program
    {
        static int GetIntFromConsole(string label)
        {
            int result;
            string input;
            do
            {
                Console.Write("{0}: ", label);
                input = Console.ReadLine();
            } while (!int.TryParse(input, out result));
            return result;
        }

        static void Main(string[] args)
        {
            int result;
            result = GetIntFromConsole("Kod");
            while (result != 1946)
            {
                Console.WriteLine("Locked");
                Thread.Sleep(4000); // 4 seconds = 4000 milliseconds
                result = GetIntFromConsole("Kod");
            } 
            Console.WriteLine("Unlocked");
        }
    }
}

As requested, i will try to explain a bit more 根据要求,我将尝试解释更多

the GetIntFromConsole can be called from anywhere inside the program, and will return only a valid parsed integer, that means empty strings, or anything else than numbers will not parse correctly, and the user will have to retry 可以从程序内的任何位置调用GetIntFromConsole,并且将仅返回有效的已解析整数,这意味着空字符串,否则数字将无法正确解析,用户将不得不重试

The loop used here, is the do { this logic } while (condition is met) 此处使用的循环是(满足条件时)执行的{this逻辑}

This means, when you are using a do / while loop you will always enter this loop, until the condition after while returns false 这意味着,当您使用do / while循环时,将始终进入此循环,直到while之后的条件返回false为止。

The while loop inside the Main, checks a condition first, and if this one is true, it goes into the loop, if not it skips forward to Console.WriteLine("Unlocked"). Main内部的while循环首先检查一个条件,如果该条件为true,则进入循环,否则返回到Console.WriteLine(“ Unlocked”)。

in case you want to run a continuous loop as mentioned in your comments below, you can to it in the following way: 如果您要运行下面的注释中提到的连续循环,可以通过以下方式进行:

static void Main(string[] args)
{
    int result;
    while (true)  // always true, it will always repeat
    {
        result = GetIntFromConsole("Kod");
        while (result != 1946)
        {
            Console.WriteLine("Locked");
            Thread.Sleep(4000); // 4 seconds = 4000 milliseconds
            result = GetIntFromConsole("Kod");
        } 
        Console.WriteLine("Unlocked");
        Thread.Sleep(4000);
    }
}

this new while loop will loop until you issue a break; 这个新的while循环将一直循环直到您发出break;为止break; statement, since it restarts the logic again from on: result = GetIntFromConsole("Kod"); 语句,因为它再次从上重新启动了逻辑: result = GetIntFromConsole("Kod"); your result will be re-evaluated and you can lock / unlock the screen again a few times ;) 您的结果将被重新评估,您可以再次锁定/解锁屏幕几次;)

I hope that was a sufficient explanation, i'm not very much of a teacher :) 我希望这是一个充分的解释,我不是一个老师:)

You can use Thread.Sleep to wait some time. 您可以使用Thread.Sleep等待一段时间。

static void Main(string[] args)
{
    while (getKod() != 1946)
    {

        System.Threading.Thread.Sleep(3000);
    }
    Console.WriteLine("Unlocked");
}


static int getKod()
{
    Console.WriteLine("Kod:");
    int kod;
    Int32.TryParse(Console.ReadLine(), out kod);
    return kod;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM