添加一个独立的计时器来跟踪程序需要多长时间,如何? [英] Add an independent Timer to keep track of how long a procedure takes, how to?

查看:127
本文介绍了添加一个独立的计时器来跟踪程序需要多长时间,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我在VB10中有一个GUI控制一些继电器(在4个或有时8个之间循环)我正在使用几个定时器来设置每个切换动作之间的延迟。

我想以某种方式投入另一个独立计时器(理想情况下显示文本框中的时间),以跟踪程序循环通过4个继电器所需的时间(这样我就可以了解多长时间)这需要重复1000次)。我想让它在循环/切换动作开始时开始计数,并在动作完成时停止计数。

此持续时间包括已知延迟我用我正在使用的计时器设置,但也包括GUI执行代码行的时间,这对我来说是未知





有人能告诉我如何实现独立计时器,可能与我使用其他计时器的方式相同吗?或者我需要使用多线程(我不熟悉)?

THX

Hey guys, I have a GUI in VB10 that''s controlling some relays (cycling between 4 or sometimes 8 of them) and I''m using several timers to set delays between each ''switch'' action.
I want to somehow throw in another independent timer (ideally display the time in a textbox) to keep track of how long the program takes to cycle through 4 relays (so that I have an idea of how long it takes to repeat this 1000 times). I want to make it start counting when the ''cycling/switching'' action begins and stop counting when the action is done.
This time duration would include the known delays that I set with the timers that I''m using, but also include the time the GUI takes to execute the lines of code, which is unknown to me.


Can someone tell me how to implement the independent timer, maybe in the same way I use other timers? or I need to use multi-threading(which I''m not familiar with)?
THX

推荐答案

不,时间程序执行时间,你根本不需要计时器。相反,你需要一个秒表: http://msdn.microsoft.com/en-us /library/system.diagnostics.stopwatch.aspx [ ^ ]。



此外,正确的时序需要了解JIT编译的工作原理:http://en.wikipedia.org/wiki/Just-in-time_compilation [ ^ ]。



您需要排除JIT时间表执行时间。如果你计算一些从未调用过的方法,或者使用在运行时尚未使用的类型或方法,那么JIT编译需要花费大量的时间。你需要避免这种新的电话。这很简单:只有在第二次重复时才会对代码片段进行计时。



祝你好运,

-SA
No, to time a procedure execution time, you don''t need a timer at all. Instead, you need a stopwatch: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^].

Also, correct timing needs some understanding of how JIT compilation works: http://en.wikipedia.org/wiki/Just-in-time_compilation[^].

You need to exclude JIT time form execution time. If you time some method which was never called, or uses types or methods that were not yet used during run time, some considerable amount of time will be spend for JIT compilation. You will need to avoid such fresh calls. This is fairly easy though: just time some fragment of code only when it is repeated second time.

Good luck,
—SA


在表单上放置一个新计时器,将间隔设置为1000(1秒)。他们在Elapsed(或Tick我永远不会记得)事件更新您的文本框。当你的进程开始时,在计时器上调用Start,并在你的计时器上完成调用Stop。



***编辑*****

哦,是的,如果你正在运行很长的进程,你应该看一下使用BackgroundWorkerProcess,这样你的主线程就不会被锁定。

http://www.dotnetperls.com/backgroundworker-vbnet [ ^ ]
Put a new timer on your form, set the interval to 1000 (1 second). Them on the Elapsed (or Tick i can never remember) event update your textbox. When your process starts call Start on the Timer and when its done call Stop on your timer.

*** EDIT *****
Oh yea, if you are running long processes you should take a look at using the BackgroundWorkerProcess so your main thread doesn''t get locked up.
http://www.dotnetperls.com/backgroundworker-vbnet[^]


这篇关于添加一个独立的计时器来跟踪程序需要多长时间,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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