秒表与计时器-何时使用 [英] StopWatch vs Timer - When to Use

查看:264
本文介绍了秒表与计时器-何时使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我这个问题,但是我似乎找不到何时使用该方法的好资料.如果您可以用简单的方式来解释它,那将是很高兴的.

Forgive me for this question, but I can't seem to find a good source of when to use which. Would be happy if you can explain it in simple terms.

此外,我正面临着这样的困境:

Furthermore, I am facing this dilemma:

看,我正在编写一个简单的应用程序.我希望它显示经过的时间(hh:mm:ss格式或其他格式).而且,为了能够加快"或减慢"其时间间隔(即加快速度,以便实时在一分钟等于应用程序中的一小时).

See, I am coding a simple application. I want it to show the elapsed time (hh:mm:ss format or something). But also, to be able to "speed up" or "slow down" its time intervals (i.e. speed up so that a minute in real time equals an hour in the app).

例如,在Youtube视频中( *我们不考虑可以跳到vid * 的特定事实),我们在左下角看到了观看该视频所花费的实际时间屏幕的一角,但是通过在选项菜单中导航,我们可以加快或加快视频的播放速度.

For example, in Youtube videos (* let's not consider the fact that we can jump to specific parts of the vid *), we see the actual time spent in watching that video on the bottom left corner of the screen, but through navigating in the options menu, we are able to speed the video up or down.

我们实际上可以看到时间的更新方式与速度因数相符(例如,如果您选择两倍的速度,则下面的计时器将比正常更新速度快两倍),并且您可以更改此速度随时随地.

And we can actually see that the time gets updated in a manner that agrees with the speed factor (like, if you choose twice the speed, the timer below gets updated twice faster than normal), and you can change this speed rate whenever you want.

这就是我所追求的.诸如Youtube视频如何测量经过的时间,以及它们可以更改时间间隔的事实.那么,您认为我应该选择哪两个呢?计时器还是秒表?

This is what I'm kinda after. Something like how Youtube videos measure the time elapsed and the fact that they can change the time intervals. So, which of the two do you think I should choose? Timer or StopWatch?

顺便说一句,我只是在编写Windows窗体应用程序.我正在模拟某些东西,我希望用户能够在他或她希望的任何时候加快速度.尽管可能很简单,但我希望采用适当的方法.

I'm just coding a Windows Form Application, by the way. I'm simulating something and I want the user to be able to speed up whenever he or she wishes to. Simple as this may be, I wish to implement a proper approach.

推荐答案

据我所知,主要区别是:

As far as I know the main differences are:

计时器

  1. 计时器只是一个简单的调度程序,偶尔会运行一些操作/方法
  2. 它在单独的线程中执行方法.这样可以防止主线程被阻塞

当我们需要在一定时间间隔内执行某些任务而不阻塞任何东西时,Timer很好.

Timer is good when we need to execute some task in certain time interval without blocking anything.

秒表

  1. 默认情况下,秒表在同一线程上运行
  2. 它计算时间并返回TimeSpan结构,这在我们需要一些其他信息时很有用
  1. Stopwatch by default runs on the same thread
  2. It counts time and returns TimeSpan struct that can be useful in case when we need some additional information

当我们需要观看时间并获得有关该方法需要花费多少处理器滴答声的其他信息时,Stopwatch是很好的选择.

Stopwatch is good when we need to watch the time and get some additional information about how much elapsed processor ticks does the method take etc.

这篇关于秒表与计时器-何时使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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