在类中使用计时器对象 [英] use timer object in class

查看:82
本文介绍了在类中使用计时器对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有很多表格,我想在每种表格中显示当前的披风.
我如何定义baseClass并在此类中定义计时器对象?
(在textBox中显示时间)

解决方案

由于计时器是一种稀缺的系统资源,我可以通过拥有一个保存了static的Clock类(或UserControl)来处理它计时器实例.每个单独的Clock实例都会将事件处理程序挂接到静态Timer,因此将使用最少的系统资源:

 公共 部分  class 时钟:UseControl
    {
    私有 静态 Timer tim =  Timer( );
    公共 静态 Clock()
        {
        tim.Tick =  1000 ; // 一秒钟的时间间隔
        tim.Start();
        }
    公共 Clock()
        {
        InitializeComponent();
        tim.Tick + =  EventHandler(tim_Tick);
        }

    无效 tim_Tick(对象发​​件人,EventArgs e)
        {
        Console.WriteLine(DateTime.Now);
        }
    } 


hi i have a lot of forms,and i want to show current cloak in each form.
how i can define baseClass and define timer object in this class?
(show time in textBox)

解决方案

Because Timers are a scarce system resource, I would handle it by having a Clock class (or UserControl) which held a static Timer instance. Each individual Clock instance would hook an event handler to the static Timer, so the minimum system resources would be used:

public partial class Clock : UseControl
    {
    private static Timer tim = new Timer();
    public static Clock()
        {
        tim.Tick = 1000;   // One second intervals
        tim.Start();
        }
    public Clock()
        {
        InitializeComponent();
        tim.Tick += new EventHandler(tim_Tick);
        }

    void tim_Tick(object sender, EventArgs e)
        {
        Console.WriteLine(DateTime.Now);
        }
    }


这篇关于在类中使用计时器对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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