zoukankan      html  css  js  c++  java
  • timer.Interval用法简介

      这个东东呢是我在做windows服务的时候碰到的,总结了一下她的用法,如下:

    一、指定时间间隔

    写一个每隔一分钟就执行一次的吧

     public partial class PSJCService : ServiceBase
        {
          
            private System.Timers.Timer timer;          
        
            public PSJCService()
            {
                InitializeComponent();
                timer = new System.Timers.Timer();
                timer.Interval = 60000//一分钟60ms*1000
                timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            }
    
            protected override void OnStart(string[] args)
            {
                timer.Enabled = true;
            }
    
            protected override void OnStop()
            {
                timer.Enabled = false;
            }
    
            void timer_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
            {
                     Console.WriteLine("每隔一分钟我就执行一次");
            }
    }    

    二、指定时间日期

    我就是想每月的一号的10:30的时候执行一次,你能拿我怎样?

     public partial class PSJCService : ServiceBase
        {
          
            private System.Timers.Timer timer;          
        
            public PSJCService()
            {
                InitializeComponent();
                timer = new System.Timers.Timer();
                timer.Interval = 60000//一分钟60ms*1000
                timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            }
    
            protected override void OnStart(string[] args)
            {
                timer.Enabled = true;
            }
    
            protected override void OnStop()
            {
                timer.Enabled = false;
            }
    
            void timer_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
            {
                     //每月1号的10:30执行一次
                    if (DateTime.Now.Day==1 && DateTime.Now.Hour==10 && DateTime.Now.Minute==30)
                    {
                        Console.WriteLine("我是每月1号的10:30,不服来咬我啊");
                    }
            }
    }    
  • 相关阅读:
    Codeforces Round #360 B
    Codeforces Round #360 C
    Codeforces Round #360 D
    新姿势 树剖求LCA
    Codeforces 165D Beard Graph 边权树剖+树状数组
    hdu3966 树链剖分+线段树 裸题
    Codeforces Round #425 D
    Codeforces Round #425 B
    Codeforces Round #425 A
    bzoj 1036 树链剖分+线段树 裸题
  • 原文地址:https://www.cnblogs.com/yunquan/p/7365236.html
Copyright © 2011-2022 走看看