zoukankan      html  css  js  c++  java
  • delegate Demo (一个关于System.Timers.Timer的Demo)

    namespace Delegates
    {
        partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                clock = new Clock(digital);
            }
    
            private void start_Click(object sender, System.EventArgs e)
            {
                this.clock.Start();
            }
    
            private void stop_Click(object sender, System.EventArgs e)
            {
                this.clock.Stop();
            }
    
            private Clock clock;
        }
    }

    Clock.cs类的内容

    namespace Delegates
    {
        using System.Windows.Forms;
    
        class Clock
        {
            public Clock(TextBox displayBox)
            {
                this.display = displayBox;
            }
    
            public void Start()
            {
                pulsed.Add(this.RefreshTime);
            }
    
            public void Stop()
            {
                pulsed.Remove(this.RefreshTime);
            }
    
            private delegate void InvokeCallback(string msg);
            private void RefreshTime(int hh, int mm, int ss)
            {
                if (!this.display.InvokeRequired)
                {
                    this.display.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", hh, mm, ss);
                }
                else
                {
                    InvokeCallback msgCallback = new InvokeCallback(DisplayText);
                    display.Invoke(msgCallback, new object[] { string.Format("{0:D2}:{1:D2}:{2:D2}", hh, mm, ss) });
    
                }
    
            }
    
            private void DisplayText(string text)
            {
                this.display.Text = text;
            }
    
            private Ticker pulsed = new Ticker();
            private TextBox display;
    
    
    
        }
    }
    namespace Delegates
    {
        using System.Collections;
        using System.Timers;
        using System.Windows.Forms;
    
        class Ticker
        {
            public delegate void Tick(int hh, int mm, int ss);
    
            public Ticker()
            {
                this.ticking.Elapsed += new ElapsedEventHandler(this.OnTimedEvent);
                this.ticking.Interval = 1000; // 1 second
                this.ticking.Enabled = true;
            }
    
            public void Add(Tick newMethod)
            {
                this.tickers += newMethod;
            }
    
            public void Remove(Tick oldMethod)
            {
                this.tickers -= oldMethod;
            }
    
            private void Notify(int hours, int minutes, int seconds)
            {
                if (this.tickers != null)
                {
                    this.tickers(hours, minutes, seconds);
                }
    
            }
    
            private void OnTimedEvent(object source, ElapsedEventArgs args)
            {
                int hh = args.SignalTime.Hour;
                int mm = args.SignalTime.Minute;
                int ss = args.SignalTime.Second;
                Notify(hh, mm, ss);
            }
    
            private Tick tickers;
            private System.Timers.Timer ticking = new System.Timers.Timer();
        }
    }

    Ticker.cs类的内容

  • 相关阅读:
    js中split字符串分割
    获取日期,实时显示当前时间,时间相减
    5.5.4 函数内部属性
    单选按钮radio和下拉选择select,ajax返回数据回显对应值
    如何在HTML不同的页面中,共用头部与尾部?
    android-Activity(四大组件之一)
    android-ImageView及其子类
    android-ActionBar
    android- 菜单
    android-Fragment
  • 原文地址:https://www.cnblogs.com/mishy/p/3042642.html
Copyright © 2011-2022 走看看