简体   繁体   English

如何在不激活C#中包含计时器的表单的情况下执行计时器控件的代码?

[英]How do I execute the code for a timer control without activating the form that contains the timer in C#?

While running the project I open three forms, one of them contains the timers. 在运行项目时,我打开三种形式,其中一种包含计时器。

While I work on another form, the project activates the form that contains the timer, due to the timer code (eg play each 10 sec). 当我处理另一种表格时,由于计时器代码(例如,每10秒播放一次),项目将激活包含计时器的表格。

How can I modify the timer code in order to work without activating the timer form? 如何在不激活计时器表格的情况下修改计时器代码才能正常工作?

Here is the code that the timer should execute: 这是计时器应执行的代码:

private void timer_Tick(object sender, EventArgs e)
{                
    btnRefresh.PerformClick();
}

You need to make the timer public ans static. 您需要将计时器设置为公共和静态。 This will initialize the timer when the code is cimpiled and make it accessible through formname.timername 锁定代码后,它将初始化计时器,并使其可以通过formname.timername进行访问。

Another other option is to create a second in the other form 另一种选择是以另一种形式创建第二个

I have used the 'Form.Activated' event to run the timer control and also I have used another event 'Form.Deactivate' event to stop the timer. 我已经使用“ Form.Activated”事件来运行计时器控件,并且还使用了另一个事件“ Form.Deactivate”事件来停止计时器。 Thank you and best regards 感谢你并致以真诚的问候

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

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