简体   繁体   English

线程上的计时器?

[英]timer on thread?

Can i create successfully a timer clock on a thread? 我可以在线程上成功创建一个计时器时钟吗? i'm creating one but it doesn't seem to work that well. 我正在创建一个,但似乎没有那么好。 My app is a multi-thread app that has to start a timer on a thread when ever a certain event happens. 我的应用程序是一个多线程应用程序,必须在某个事件发生时在线程上启动计时器。 The timer is placed in every client connection. 计时器放在每个客户端连接中。 The clock doesn't work until i close my winform(i don't know why) . 时钟不起作用,直到我关闭我的winform(我不知道为什么)。 Is there anything in particular that i should know about timers in threads ? 有什么特别的东西,我应该知道线程中的计时器?

Here is my timer code : 这是我的计时器代码:

timer1.Interval = 1000;
timer1.Tick += new EventHandler(timer_Tick);
timer1.Enabled = true;
timer1.Start();

You might try to use System.Threading.Timer , which is basically a timer that's in a separate thread. 您可能尝试使用System.Threading.Timer ,它基本上是一个单独的线程中的计时器。 You might also consider using a WaitHandle that's never fired and then using WaitOne(1000, false) to wait for a second. 您也可以考虑使用从未触发的WaitHandle ,然后使用WaitOne(1000, false)等待一秒钟。

Try this: 尝试这个:

public void EnableTimer()
        {
            if (this.InvokeRequired)
                this.Invoke(new Action(EnableTimer));
            else
                this.timer1.Enabled = true;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM