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类的内容

  • 相关阅读:
    数据库迁移至ASM
    获取数据库或SHEME的DDL语句
    membership配置数据库(SQL2000)
    DIV+CSS到底是什么?
    如何更改表的所有者权限
    windows server 2003 上“您要访问的网页有问题,无法显示。HTTP 500 内部服务器错误。”的问题解决方案!
    瞎忙
    瞎忙
    如何更改表的所有者权限
    DIV+CSS到底是什么?
  • 原文地址:https://www.cnblogs.com/mishy/p/3042642.html
Copyright © 2011-2022 走看看