• 实现计数器、定时器的3种方法


    using System;
    using System.Threading;
    using System.Windows.Forms;

    namespace CallFunction
    {
        /// <summary>
        /// 多线程示例,manual thread
        /// 利用Timmer类实现计数器
        /// </summary>
        public partial class Form2 : Form
        {
            public delegate void SetControlValue(string value);//1.自定义委托
            public int cur = 0;
            System.Timers.Timer time;//Timer组件变量
            System.Threading.Timer threadtime;
            //Timer time;//容器变量,Forms.Timer和Winform的窗体(主用户接口线程)共用一个线程,timer1.class,执行时间长易出现页面假死现象,误差55ms
            public Form2()
            {
                InitializeComponent();
                Control.CheckForIllegalCrossThreadCalls = false;//关闭线程安全,跨线程
            }

            private void Form2_Load(object sender, EventArgs e)
            {
                InitTimer();
            }

            private void InitTimer()
            {
                //throw new NotImplementedException();
                time = new System.Timers.Timer(1000);//每隔1s
                time.AutoReset = true;
                time.Enabled = true;
                //绑定Elapsed事件
                //Elapsed为事件委托类型变量
                // time.Elapsed += new System.Timers.ElapsedEventHandler(TimerUp);//2、直接注册事件,事件委托类型代理,绑定事件变量,即每经过/隔1s自动执行的事件委托类型方法。
                 threadtime = new System.Threading.Timer(new TimerCallback(TimerUp1), null, Timeout.Infinite, 1000);//使用callback,每隔1s执行回调方法TimerUp1

            }
            /// <summary>
            /// 事件委托的区别:事件是一种特殊的委托,意味着事件委托类型可以关联多个事件,即将多个委托注册到事件委托类型,事件委托类型维持了一组委托的地址;而委托可以关联多个方法;即将多个方法注册到委托类型 ,委托类型维持了一组方法的地址。
            /// 委托是方法的封装;事件是委托的封装
            /// 事件是一个变量,可由委托初始化无须显式实例化;委托是一种类型,初始化需用方法实例化
            /// </summary>
            /// <returns></returns>
            private Delegate getdel() {
                SetControlValue sc = null;
                sc = new SetControlValue(SetTextBoxText);//委托需先实例化
                sc += new SetControlValue(show);
                return sc;
            }
            private void TimerUp1(object state)
            {
                // throw new NotImplementedException();
                try
                {
                    cur += 1;
                    //SetControlValue sc = null;
                    //sc = new SetControlValue(SetTextBoxText);//委托需先实例化
                    //sc += new SetControlValue(show);
                    //sc(cur.ToString());//执行方法
                    //this.Invoke(sc, cur.ToString());//执行方法
                    //this.Invoke(new SetControlValue(SetTextBoxText),cur.ToString());////执行方法,invoke方法触发事件,执行委托

                    this.Invoke(getdel(), cur.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show("执行定时到点事件失败:" + ex.Message);
                }
            }

            private void TimerUp(object sender, System.Timers.ElapsedEventArgs e)//3、自定义事件
             {
                 try
                 {
                     cur += 1;
                    SetControlValue sc = null;
                    sc = new SetControlValue(SetTextBoxText);//委托需先实例化
                    sc += new SetControlValue(show);
                    sc(cur.ToString());
                    this.Invoke(sc,cur.ToString());
                    //this.Invoke(new SetControlValue(SetTextBoxText),cur.ToString());//invoke方法触发事件,执行委托
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show("执行定时到点事件失败:" + ex.Message);
                 }
             }

            private void show(string value)
            {
                //throw new NotImplementedException();
                this.textBox2.Text = cur.ToString();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                time.Start();
            }

            private void SetTextBoxText(string v)//4、自定义委托的关联方法
            {
                this.textBox1.Text = v.ToString();
            }

            private void button2_Click(object sender, EventArgs e)
            {
                time.Stop();
            }

            private void button3_Click(object sender, EventArgs e)
            {
                //立即开始计时,时间间隔1000毫秒
                threadtime.Change(0, 1000);// 等待时间为0;间隔时间 1s
            }

            private void button4_Click(object sender, EventArgs e)
            {
                //停止计时
                threadtime.Change(Timeout.Infinite, 1000);
            }
        }
    }

  • 相关阅读:
    zookeeper项目使用几点小结
    Dubbo架构设计详解
    关于web.xml不同版本之间的区别
    bat定时执行,清除PHP缓存
    新闻列表标题省略样式
    php把时间格式化
    HTML5中的拖放
    Redis JAVA客户端 Jedis常用方法
    Redis 简介
    SpringBoot DataSource 配置说明
  • 原文地址:https://www.cnblogs.com/80028366local/p/12765364.html
Copyright © 2020-2023  润新知