如何在控制台应用程序中创建一个简单的计时器 [英] How to create a simple timer in the console application

查看:73
本文介绍了如何在控制台应用程序中创建一个简单的计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个非常简单的计时器,只需在您键入start时启动,只需在键入stop时停止,然后在控制台应用程序中显示以小时分和秒为单位的经过的时间。非常感谢帮助。谢谢。



我尝试了什么:



我试过在线找到的其他解决方案都没有。

解决方案

计时器用于在一段时间过后触发一些代码。



从您的描述中,您想要测量两个事件之间的时间。一个秒表将是最好的方法:

Stopwatch Class(System.Diagnostics)| Microsoft Docs [ ^ ]



或者,如果您不太担心精度,请存储 DateTime.UtcNow 当用户键入start时,当他们键入stop时,从 DateTime.UtcNow 中减去该值并打印出结果。


< blockquote>在文档中( Timer Class(System.Timers) )| Microsoft Docs [ ^ ])您可以找到示例代码。


I need a very simple timer that starts simply when you type "start" and stops simply when you type "stop" then displays the amount of time elapsed in hours minutes and seconds in the console application. Help will be very much appreciated. Thank you.

What I have tried:

I have tried other solutions found online and none have worked.

解决方案

A timer is for triggering some code after a period of time has elapsed.

From your description, you want to measure the time between two events. A Stopwatch would be the best way to do that:
Stopwatch Class (System.Diagnostics) | Microsoft Docs[^]

Or, if you're not too worried about the precision, store DateTime.UtcNow when the user types "start", and when they type "stop", subtract that from DateTime.UtcNow and print out the result.


In the documentation ( Timer Class (System.Timers) | Microsoft Docs[^]) you may find sample code.


这篇关于如何在控制台应用程序中创建一个简单的计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆