zoukankan      html  css  js  c++  java
  • 定时器

    在一个应用程序中,要制作一个在某个时间点触发时间。比如说:在每天的11:点去插入一条语句。下面是.net1.1中的示例。

    Global.asax.cs

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Web;
    using System.Web.SessionState;
    using System.Timers;

    namespace Kaoqin
    {
     /// <summary>
     /// Global 的摘要说明。
     /// </summary>
     public class Global : System.Web.HttpApplication
     {
      /// <summary>
      /// 必需的设计器变量。
      /// </summary>
      private System.ComponentModel.IContainer components = null;

      public Global()
      {
       InitializeComponent();
      } 
      
      protected void Application_Start(Object sender, EventArgs e)
      {
       System.Timers.Timer aTimer = new System.Timers.Timer();
       aTimer.Elapsed += new ElapsedEventHandler(Timer.TimeEvent);
       // 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
       aTimer.Interval = 1200000;//20分钟检查一次。
       aTimer.Enabled = true;
      }
     
      protected void Session_Start(Object sender, EventArgs e)
      {

      }

      protected void Application_BeginRequest(Object sender, EventArgs e)
      {

      }

      protected void Application_EndRequest(Object sender, EventArgs e)
      {

      }

      protected void Application_AuthenticateRequest(Object sender, EventArgs e)
      {

      }

      protected void Application_Error(Object sender, EventArgs e)
      {

      }

      protected void Session_End(Object sender, EventArgs e)
      {

      }

      protected void Application_End(Object sender, EventArgs e)
      {
       //结束
      }
       
      #region Web 窗体设计器生成的代码
      /// <summary>
      /// 设计器支持所需的方法 - 不要使用代码编辑器修改
      /// 此方法的内容。
      /// </summary>
      private void InitializeComponent()
      {   
       this.components = new System.ComponentModel.Container();
      }
      #endregion
     }
    }

     

    Timer.cs

    using System;
    using System.Timers;
    using System.Data.SqlClient;

    namespace Kaoqin
    {
     /// <summary>
     /// Timer 的摘要说明。
     /// </summary>
     public class Timer
     {
      public static void TimeEvent(object source, ElapsedEventArgs e)
      {  
       // 得到 hour minute second  如果等于某个值就开始执行某个程序。
       int intHour   = e.SignalTime.Hour;
    //   int intMinute = e.SignalTime.Minute;
    //   int intSecond = e.SignalTime.Second;
       // 定制时间; 比如 在23:00 :00 的时候执行某个函数
       int iHour   = 13;
    //   int iMinute = 50;
    //   int iSecond = 00;
       // 设置  每秒钟的开始执行一次
       //   if( intSecond == iSecond )
       //   {
       //    Console.WriteLine("每秒钟的开始执行一次!");
       //   }
       // 设置 每个小时的30分钟开始执行
       //   if( intMinute == iMinute && intSecond == iSecond )
       //   {
       //    Console.WriteLine("每个小时的30分钟开始执行一次!");
       //   }
      
       // 设置 每天的23:00:00开始执行程序
       if( intHour == iHour)// && intMinute == iMinute  && intSecond == iSecond )
       {

        DateTime logdate=DateTime.Now;

        SqlConnection myconn=new SqlConnection("server=(local);database=Kaoqin;uid=sa;pwd=;");
        string insertlog="insert into LoginLog(EmployeeId,Date) values('1111111','"+logdate+"')";
        SqlCommand scminsertlog=new SqlCommand(insertlog,myconn);
        myconn.Open();
       
        scminsertlog.ExecuteNonQuery();//.ExecuteADU(insertlog);
        scminsertlog.Dispose();
        myconn.Close();
       }
      }
     }
    }

  • 相关阅读:
    解决com.xpand.. starter-canal 依赖引入问题
    缓存预热加入二级缓存
    缓存预热的实现
    ShardingSphere 中有哪些分布式主键实现方式?
    ShardingSphere 如何实现系统的扩展性
    如何系统剖析 ShardingSphere 的代码结构?
    SharingSphere的数据脱敏
    ShardingSphere的分布式事务
    Qt 事件过滤器原理(installEventFilter函数)
    Qt Event 以及 Event Filter 事件处理
  • 原文地址:https://www.cnblogs.com/qfb620/p/1350237.html
Copyright © 2011-2022 走看看