如何从不同的线程/类启用计时器 [英] How to enable a timer from a different thread/class

查看:19
本文介绍了如何从不同的线程/类启用计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原帖:如何访问来自 C# 中另一个类的计时器

我什么都试过了.

-事件

-无法完成调用,因为计时器没有 InvokeRequired 属性.

-Invoke can't be done,because Timers don't have InvokeRequired property.

-公共/内部财产

没有任何效果,代码正在执行,timer.Enabled 属性被设置为true",但它不打勾.如果我调用事件或只是在非表单类中更改属性 -静态方法 - 它确实有效.

Nothing worked,the code is being executed,the timer.Enabled property is being set to "true" ,but it doesn't Tick.If I call the event or just change the property from the form class in a NON-static method - it does tick and works.

我从不知道要学会如何使用合适的计时器需要一天甚至更多的时间.

I never knew it would take me a day and probably more to acquire how to use a decent timer.

如果没有办法做到这一点,还有什么我可以使用的类似于计时器的工作(延迟,启用/禁用)?

If there's no way to do this,is there anything else I can use that works similiar to the timer(delay,enable/disable)?

推荐答案

如果您想要多线程支持,您应该使用 System.Timers 命名空间中的 Timer 类,而不是 WinForms Timer 控件.有关详细信息,请查看 WinForms Timer 控件的 MSDN 文档:

You should use the Timer class from the System.Timers namespace, and not the WinForms Timer control, if you want multi-threading support. Check the MSDN documentation for the WinForms Timer control for more information:

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

这篇关于如何从不同的线程/类启用计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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