EventHandler和Timer添加事件操作

本文介绍了在使用EventHandler时,避免重复添加事件以防止多次执行的重要性。同时提到了Timer计时器的使用,强调了在添加事件后设置计时器为非活动状态以防止无限循环执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Go
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            checkEH += OnPlus;
            checkEH += OnOp;
            InitializeComponent();
            Initial();
        }
        public delegate void checkEnventHandler(object sender, checkEventArgs e);
        public event checkEnventHandler checkEH;
        public class checkEventArgs : EventArgs
        {
            public string str = "Hello World!";
            public checkEventArgs(string str)
            {
                this.str = str;
            }
            //MessageBox.Show(str);
            ~checkEventArgs()
            {}
        }

        public void OncheckEH(checkEventArgs e)
        {
            checkEH(this, e);
        }
        public int a = 0;
        public int b = 0;
        public string strOp = "String OpVeal";
        public void OnOp(object sender, checkEventArgs e)
        {
            b = 0;
            b++;
            strOp = "/////*****";
        }
        public void OnPlus(object sender, checkEventArgs e)
        {
            a = 9;
        }
        private void btn_Click(object sender, EventArgs e)
        {
            
            checkEventArgs msg = new checkEventArgs("HOO");
            OncheckEH(msg); 
            txt.Text += strOp;
            txt.Text += "*******";
            txt.Text += a.ToString();
            txt.Text += "*******";
            txt.Text += b.ToString();
            timer2.Enabled = true;
        }
        public System.Timers.Timer timer1 = new System.Timers.Timer();
        public System.Timers.Timer timer2 = new System.Timers.Timer();
        public List<System.Timers.Timer> lsttimer = new List<System.Timers.Timer>();
        public void Get1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            a++;
            if (a>46)
            {
                timer1.Enabled = false;
                timer2.Enabled = true;
            }

        }
        public void Get2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            b++;
            MessageBox.Show("Waiting...");
            if (b > 10)
            {
                timer2.Enabled = false;
            }
        }
        public void Initial()
        {
            timer1 = new System.Timers.Timer(5000);
            timer1.Elapsed += new System.Timers.ElapsedEventHandler(Get1_Elapsed);
            lsttimer.Add(timer1);
            timer2 = new System.Timers.Timer(1000);
            timer2.Elapsed += new System.Timers.ElapsedEventHandler(Get2_Elapsed);
            lsttimer.Add(timer2);
        }
    }
}

在EventHandler中添加事件时,不能重复执行添加事件函数(Initial),否则导致EventHandler中多次添加该事件,会重复执行。

Timer计时器中添加事件,如果不将计时器的使能置为false,则会一直执行下去。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值