简体   繁体   English

C#-遍历表单组件(不是控件)

[英]C# - Iterate through form components (not controls)

My form has a number of Timer components that I am seeking to loop through at runtime. 我的表单包含许多要在运行时循环访问的Timer组件。 My goal is to stop all timers on the form at once. 我的目标是立即停止表单上的所有计时器。

I tried adapting some code I had to do the same thing with controls however it doesn't appear to be working. 我尝试调整一些代码,我必须对控件执行相同的操作,但是似乎无法正常工作。

foreach (var tmr in this.components.Components.OfType<Timer>())
{
    tmr.Stop();
}

Any help would be appreciated. 任何帮助,将不胜感激。

Stopping a Winforms Timer means that no more Timer messages (WM_TIMER) are posted to the message queue. 停止Winforms计时器意味着没有更多的计时器消息(WM_TIMER)被发布到消息队列。 But any previous WM_TIMER message still in the message Queue will be processed. 但是,仍将在消息队列中处理任何先前的WM_TIMER消息。 So multiple Tick Events might be processed even after the Timer has been stopped when you have created Timer messages faster then they can be processed. 因此,当您以更快的速度创建计时器消息后,即使停止计时器后,也可能会处理多个滴答事件,然后才可以对其进行处理。

Update: Checking the source reveals that the Winforms Timer swallows WM_TIMER messages when the timer has been stopped and won't fire any Tick Events then. 更新:检查源发现Winforms计时器停止计时器时,它会吞下WM_TIMER消息,然后不会触发任何Tick事件。 So this is not the correct answer. 因此,这不是正确的答案。

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

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