.net c#可以暂停程序时间吗?

Is it possible to time a break from a program?
2021-02-23
  •  译文(汉语)
  •  原文(英语)

我已经学习C#已有3周了,所以仍然涵盖了基础知识.

我的问题:

在程序结束时(例如,一个简单的货币转换器),当用户选择退出该程序(而不是执行其他计算)时,是否可以定时将Console.Writeline设置为在休息前保持可见?

码:

 Console.WriteLine("Would you like to perform another conversion?");
 Console.WriteLine("");
 Console.WriteLine("y for YES");
 Console.WriteLine("e for EXIT");
 decision = Console.ReadLine();

 if (decision.Equals("E", StringComparison.OrdinalIgnoreCase))
 {

 Console.WriteLine("Thank-you for using CurrencyConverter");
 break;

 ...

//在程序终止之前,上面的写入行是否可以绕几秒钟以提高可读性?

速聊1:
只是一个样式问题,为什么在if语句中出现中断?那可能不应该在那里...
解决过程1

是的,绝对:您可以使用System.Threading.Thread.Sleep(3000),其中参数是延迟(以毫秒为单位).

速聊1:
非常感谢您的回答
速聊2:
没问题
解决过程2

我建议您简单地使用Thread.Sleep(milliseconds)添加System.Threading到用法中的可用方法.从字面上看,它正在使当前正在处理程序的线程进入睡眠状态.

PS:除非给定的代码处于循环中,否则break不应在if语句中,因为break通常用于在特定点脱离循环.如果您要退出程序,建议您使用return;,如果您使用的是main方法或使用Environment.Exit(0);

解决过程3

您可以使用System.Threading.Sleep(int)函数来指定要睡眠的毫秒数.

还有一些其他方法可以使程序等待.例如,您可以让程序等到完成输出读取为止.

一种方法是等待控制台输入:

Console.ReadKey();

或者,如果您使用"开始而不调试"而不是"开始调试",则程序将自动中断并等待按键,然后关闭命令提示符窗口.

速聊1:
感谢LVBen,很高兴知道.我一直在寻找一种无需按下键即可完成此操作的方法,但这是有用的信息:)

I've been learning C# for 3 weeks now so still covering the basics.

My question:

At the end of a program (a simple currency converter, for example), when the user chooses to exit the program (rather than perform another calculation) is it possible to time a Console.Writeline to remain visible before a break?

Code:

 Console.WriteLine("Would you like to perform another conversion?");
 Console.WriteLine("");
 Console.WriteLine("y for YES");
 Console.WriteLine("e for EXIT");
 decision = Console.ReadLine();

 if (decision.Equals("E", StringComparison.OrdinalIgnoreCase))
 {

 Console.WriteLine("Thank-you for using CurrencyConverter");
 break;

 ...

//is it possible to have the writeline above hang around for a couple of seconds for readability before the program terminates?

Talk1:
Just a style question, why do you have a break in an if statement? That probably shouldn't be there...
Solutions1

Yes, absolutely: you can use System.Threading.Thread.Sleep(3000), where the argument is the delay in miliseconds.

Talk1:
Thanks a lot for your answer
Talk2:
No problem
Solutions2

I suggest you simply use Thread.Sleep(milliseconds) which is available once you add System.Threading to your usings. It is literally putting the thread which is currently taking care of your program into sleep.

PS: Break should not be in your if statement unless the code you have given is in a loop, because break is normally used to break out of loops at a certain point. If you are trying to exit from the program I suggest you rather use return;, if you are in the main method or by using Environment.Exit(0);

Solutions3

You can use the System.Threading.Sleep(int) function to specify the number of milliseconds to sleep for.

There are also some alternatives to causing the program to wait. For example, you can make the program wait until you are done reading the output.

One way to do that is to wait for console input with:

Console.ReadKey();

Or, if you use "Start Without Debugging" instead of "Start Debugging", then the program will break automatically and wait for a key press before closing the command prompt window.

Talk1:
Thanks LVBen, good to know. I was looking for a way to do it without having to press keys but this is useful information :)
转载于:https://stackoverflow.com/questions/23713107/is-it-possible-to-time-a-break-from-a-program

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入