定时器功能不听取锁定 [英] Timer function doesn't listen to lock

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

问题描述

我将Timer类设置为每秒触发一次。每秒调用的Tick函数

使用一个锁来防止

同时执行相同代码的多个tick。其中的代码调用一个

Visual Basic输入消息框,它是同步的,它等到你输入一些输入后再按
,然后按回车键。但是,这个消息框每秒弹出

,无论旧的消息是否已经完成。计时器'的
Tick函数被反复调用,并在锁中运行代码,

完全忽略了锁。为什么?线程有什么关系?但是,

这就是锁是FOR的。锁定变量=新对象(),所以

它应该没问题。


Zytan

I have a Timer class set to trigger every second. The Tick function
that is called every second uses a lock to prevent multiple ticks from
executing the same code at the same time. The code within calls a
Visual Basic input message box, which is synchronous, it waits until
you enter some input, and press enter. But, this message box pops up
every second regardless if the old ones have finished. The timer''s
Tick function is called over and over, and runs the code in the lock,
totally ignoring the lock. Why? Something to do with threads? But,
that''s just what locks are FOR. The lock variable = new object(), so
it should be fine.

Zytan

推荐答案

顺便说一句,我正在使用System.Windows.Forms.Timer。


我应该只使用System.Timers.Timer,并使用

Timer.AutoReset,只让时间勾选一次,我会在它滴答之后重启

? (这应该是一个解决方案,但是我仍然希望

知道锁具上发生了什么......上面没有工作......)


Zytan
Btw, I''m using System.Windows.Forms.Timer.

Should I just use System.Timers.Timer, instead, and use
Timer.AutoReset, to make the time tick ONLY once, and I''ll start it up
again after it ticks? (That should be a solution, but I''d still like
to know what is going on with the lock that''s not working above......)

Zytan


2008年7月16日星期三14:25:54 -0700,Zytan< zy ********* *@gmail.com写道:
On Wed, 16 Jul 2008 14:25:54 -0700, Zytan <zy**********@gmail.comwrote:

我将Timer类设置为每秒触发一次。每秒调用的Tick函数

使用一个锁来防止

同时执行相同代码的多个tick。其中的代码调用一个

Visual Basic输入消息框,它是同步的,它等到你输入一些输入后再按
,然后按回车键。但是,这个消息框每秒弹出

,无论旧的消息是否已经完成。计时器'的
Tick函数被反复调用,并在锁中运行代码,

完全忽略了锁。为什么?线程有什么关系?但是,

这就是锁是FOR的。锁变量=新对象(),所以

它应该没问题。
I have a Timer class set to trigger every second. The Tick function
that is called every second uses a lock to prevent multiple ticks from
executing the same code at the same time. The code within calls a
Visual Basic input message box, which is synchronous, it waits until
you enter some input, and press enter. But, this message box pops up
every second regardless if the old ones have finished. The timer''s
Tick function is called over and over, and runs the code in the lock,
totally ignoring the lock. Why? Something to do with threads? But,
that''s just what locks are FOR. The lock variable = new object(), so
it should be fine.



你的问题很模糊。但我会猜一猜。


无论你使用什么计时器类(.NET中不少于3个
和你' 我没有具体说明你正在使用哪一个......我猜是这样的。
System.Windows.Forms.Timer,因为那个我知道的就是我的头脑。 >
有一个Tick事件),毫无疑问每次都会在相同的

线程上引发Tick事件。 lock()语句只能阻止多个线程

输入相同的代码。任何给定的线程,一旦它成功获得锁定,就可以重新输入相同的锁() - 受保护的代码任意多次b / b $

/>
如果您不希望在计时器的每个刻度显示消息框,

只需在显示消息框之前禁用计时器并重新启用它
$消息框被取消时b $ b。你不能将lock()用于此目的

你试图使用它。


为了将来的参考,调试器是一个很好的方式来探索实际上正在进行的
。例如,在这种特殊情况下,您可以设置一个断点来显示消息框。

。然后,第二个

时间到达断点,你可以查看调用堆栈。在那个

点,你会发现在同一个调用堆栈中,上一次调用

显示了消息框。


有了这些信息,你就可以成功地推断每次在同一个帖子中显示消息

框,你可以从中推断出b
lock()语句不会有帮助。即使你对bn(b)的工作原理有点不确定,但只知道实际发生的事情可能会让你觉得啊哈! "

需要时刻解决问题。 :)


Pete

Your question is vague. But I''ll make a guess.

Whatever timer class you''re using (there are no less than three in .NET
and you''re not specific about which one you''re using...I''m guessing
System.Windows.Forms.Timer, since that one I know off the top of my head
has a Tick event), undoubtedly the Tick event is being raised on the same
thread each time. The lock() statement only prevents multiple threads
from entering the same code. Any given thread, once it''s successfully
obtained the lock, can reenter the same lock()-protected code arbitrarily
many times.

If you don''t want the message box displayed with each tick of the timer,
just disable the timer before you show the message box and reenable it
when the message box is dismissed. You can''t use lock() for the purpose
you''re trying to use it for.

For future reference, the debugger is a great way to explore what''s
actually going on. For example, in this particular case, you could have
just set a breakpoint where you show the message box. Then, the second
time you hit the breakpoint, you could look at the call stack. At that
point, you''d find that in the same call stack was the previous call that
showed the message box.

With that information, you could then successfully reason that the message
box is getting shown in the same thread each time, from which you could
then infer that the lock() statement isn''t going to help. Even if you
were a bit uncertain about the rules for how lock() works, just knowing
what was actually going on would probably have given you that "aha!"
moment needed to figure things out. :)

Pete


7月16日,5:25 * pm,Zytan< zytanlith ... @ gmail。 comwrote:
On Jul 16, 5:25*pm, Zytan <zytanlith...@gmail.comwrote:

我有一个Timer类设置为每秒触发一次。 *每秒调用的Tick函数

使用一个锁来防止来自同时执行相同代码的多个tick。

。 *内部代码调用一个

Visual Basic输入消息框,它是同步的,它等到你输入一些输入后再按
,然后按回车键。 *但是,这个消息框每秒弹出

,无论旧的消息是否已经完成。 *计时器'的
Tick函数被反复调用,并在锁中运行代码,

完全忽略了锁定。 *为什么? *与线程有关? *但是,

'就是锁是FOR的。 *锁定变量=新对象(),所以

它应该没问题。

I have a Timer class set to trigger every second. *The Tick function
that is called every second uses a lock to prevent multiple ticks from
executing the same code at the same time. *The code within calls a
Visual Basic input message box, which is synchronous, it waits until
you enter some input, and press enter. *But, this message box pops up
every second regardless if the old ones have finished. *The timer''s
Tick function is called over and over, and runs the code in the lock,
totally ignoring the lock. *Why? *Something to do with threads? *But,
that''s just what locks are FOR. *The lock variable = new object(), so
it should be fine.

Zytan



您好,


您无法从UI以外的线程调用UI。你在用

Control.Invoke?

Hi,

You cannot call a UI from a thread other than the UI. Are you using
Control.Invoke?


这篇关于定时器功能不听取锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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