C#计时器问题......... [英] C# timer problem.........

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

问题描述


有没有可能只用秒来使用计时器....
例如标准计时器是这样的:("{0:hh:mm:ss tt}表示00:00:00
h m s



我只想举第二个例子,例如"00"

thnx ??

解决方案

您需要日期时间或时间跨度?

 //  datetime:parenthesys是不必要的,只是为了强调您可以在其中添加日期时间他们.
 var  s =(DateTime.Now).第二

//  timespan:1h:2m:3的时间跨度,与以前的parenthesys一样,您可以将datediff(DateTime .Now.Substract(yourDate))
 var  s =( TimeSpan( 1  2  3 )).TotalSeconds; 


<我认为您可能会混淆对象.计时器用于通过 Interval属性 [ ^ ]

此属性以毫秒为单位,因此,如果要使用秒进行设置,只需将秒值乘以1000.


如果您要问的是秒表,则只需使用ElapsedTime(这是一个时间跨度)并访问总秒数即可(如katananesei所说).

 System.Diagnostics.Stopwatch sw =  System.Diagnostics.Stopwatch();

sw.Start();
// 执行您想要捕获的处理时间
sw.Stop();
 var  seconds = sw.Elapsed.TotalSeconds; 


Hi
is there any possibility to use timer only with seconds....
for example standard timer is this : ("{0:hh:mm:ss tt} that mean 00:00:00
h m s



I want to have example only second ex " 00 "

thnx ??

解决方案

You need a datetime or a timespan ?

//datetime : parenthesys are unnecessary, just to underline that you can put a datetime in them.
var s = (DateTime.Now).Second 

// timespan : time span of 1h : 2m : 3, as before parenthesys are unnecessary, you can put a datediff (DateTime.Now.Substract(yourDate))
var s = (new TimeSpan(1, 2, 3)).TotalSeconds;


I think you might be confusing objects. The timer is used for intervals which is set via the Interval Property[^]

This property is in milliseconds so if you want to set it using seconds just multiply your seconds value by 1000.


If it is the stopwatch you are asking about then just use the ElapsedTime which is a time span and access the total seconds (as katananesei has said).

System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

sw.Start();
//Do what you are trying to capture process time on
sw.Stop();
var seconds = sw.Elapsed.TotalSeconds;


这篇关于C#计时器问题.........的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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