using System;
using System.Timers;

namespace TimerTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Timer time = new Timer(); //引用一个实例
            time.Interval = 1000;//Interval代表每间隔多少时间,毫秒.1000就是1秒
            Boy boy = new Boy();
            Girl girl = new Girl();
            time.Elapsed += boy.Action; //写好了Boy.方法之后,直接ctrl+.  它会自动帮你在该类内创建Action的方法
            time.Elapsed += girl.Action;
            time.Start();//启动时钟
            Console.ReadLine();
        }
    }
    class Boy
    {
        internal void Action(object sender, ElapsedEventArgs e)//ElapsedEventArgs是自动添加的,记住让他自动添加,因为比如Click方法的点击事件却不是ElapsedEventArgs
        {
            Console.WriteLine("这不夹子吗");
        }
    }
    class Girl
    {
        internal void Action(object sender, ElapsedEventArgs e)
        {
            Console.WriteLine("吃饭了没呀!");
        }
    }
}

最后修改:2022 年 12 月 05 日
如果觉得我的文章对你有用,请随意赞赏