需要帮助以使用Timer对象 [英] need help to work with Timer object

查看:70
本文介绍了需要帮助以使用Timer对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI ...
我想在列表视图上显示当前时间,该时间会随着运行Windows的时钟而自动更改...

HI...
I want to display the current time on a listview, this time automatically changes with the clock running Windows...

推荐答案

使用任何计时器都会给您带来更多麻烦比线程-强烈建议.
只是给您一个想法:您是否计划了在下一个事件被触发时尚未退出计时器事件处理程序的情况?还有许多其他陷阱.最差的是System.Windows.Forms.Timer.

使用System.Threading.Thread.Sleep在所需的时间量内创建无限循环运行的线程.您可以基于此重复触发任何UI操作,但是请记住,您不能直接从此非UI线程调用UI上的任何内容.相反,您应该使用System.Threading.DispatcherSystem.Windows.Forms.ControlInvokeBegingInvoke方法(绝对不要紧什么控件实例,它应该是真正参与您的UI的任何控件,例如您的表单).在我过去的答案中,您会找到有关其工作原理的非常详细的解释和进一步的指导:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [
Using any kind of timer will give you more trouble than a thread — highly recommended.
Just to give you an idea: did you plan for situations when your timer event handler is not yet exited at the moment the next one if fired? There are many other traps. The worst one is System.Windows.Forms.Timer.

Create a thread infinitely running in a loop, using System.Threading.Thread.Sleep for the required amount of time. Your can trigger any UI operation based on this repetition, but remember, you cannot call anything on the UI directly from this non-UI thread. Instead, you should use the methods Invoke or BegingInvoke of System.Threading.Dispatcher or System.Windows.Forms.Control (absolutely does not matter what instance of control, it should be any control really participating in you UI, for example, your form). You will find very detailed explanation of how it works and further directions in my past Answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

Just in case: my collection of references to my past Answers on threads, could be very useful:
How to get a keydown event to operate on a different thread in vb.net[^].

—SA


只需使用System.Timers.Timer对象,然后在tick事件处理程序中,更新您的列表视图.
Just use a System.Timers.Timer object, and in the tick event handler, update your listview.


在表格中插入一个计时器,然后双击计时器图标,然后在计时器功能中编写以下代码:
注意:设置启用计时器并设置间隔100.
这是一个计时器功能:

Insert a Timer in form and double click on timer icon and in Timer function write this code :
note : set enable timer and set interval 100.
This a timer function :

private void timer1_Tick(object sender, EventArgs e)
       {
           Label.Text = DateTime.Now.ToLongTimeString();
       }


这篇关于需要帮助以使用Timer对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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