C# 实现每隔规定的时间段自动执行程序

来源:互联网 发布:男士礼服知乎 编辑:程序博客网 时间:2024/05/17 08:46

方法一: 使用System.Timers.Timer 类

  System.Timers.Timer time = new System.Timers.Timer(30000); //实例化Timer类,规定每隔30秒执行一次            time.Elapsed +=new System.Timers.ElapsedEventHandler(aa); //当达到规定的时间内执行aa 这个方法            time.AutoReset = true;//false 执行一次,true 一直执行            time.Enabled = true;//设置是否执行time.Elapsed 时间   private void aa(object source, System.Timers.ElapsedEventArgs e)   {//方法体   }

方法二:使用线程执行方法,在方法中实现死循环,在每个循环中执行join 方法

Thread testThread = new Thread(bb);testThread.Start();private void bb(){     while (true)      {        //方法体        testThread.Join(30000); //设定阻止时间                     }}

以上两种方法比较常见也比较简单。可在实际中使用。

0 0
原创粉丝点击