zoukankan      html  css  js  c++  java
  • ASP.NET 定时执行任务(定时器)

    ASP.NET 定时执行任务(定时器)[日期:2010-12-15] 来源:Net探索者  作者:未知 [字体:大 中 小]


    服务器端采用C#语法:
    1.在Global.asax文件中导入命名空间
    <%@ Import Namespace="System.Timers" %>

    2.Global.asax文件中的Application_Start()方法内写如下代码:
    System.Timers.Timer objTimer = new Timer();
    objTimer.Interval = 时间; //这个时间单位毫秒,比如10秒,就写10000
    objTimer.Enabled = true;
    objTimer.Elapsed += new ElapsedEventHandler(objTimer_Elapsed);

    3.Global.asax文件中添加一个方法
    void objTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
    //这个方法内实现你想做的事情。
    //例如:修改Application的某一个值等等。
    }
    以上3步则可以在指定
    using System;
    using System.Timers;
    namespace 定时器ConsoleApplication1
    {
    class Class1
    { 
     [STAThread] 
     static void Main(string[] args)
     {
      System.Timers.Timer aTimer = new System.Timers.Timer();
      aTimer.Elapsed += new ElapsedEventHandler(TimeEvent);
      // 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
      aTimer.Interval = 1000;
      aTimer.Enabled = true;
      Console.WriteLine("按回车键结束程序");
      Console.WriteLine(" 等待程序的执行......");
      Console.ReadLine();
     }
     // 当时间发生的时候需要进行的逻辑处理等
    //  在这里仅仅是一种方式,可以实现这样的方式很多.
    private 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;
      // 定制时间; 比如 在10:30 :00 的时候执行某个函数
      int iHour  = 10;
      int iMinute = 30;
      int iSecond = 00;
      // 设置  每秒钟的开始执行一次
      if( intSecond == iSecond )
      {
      Console.WriteLine("每秒钟的开始执行一次!");
      }
      // 设置 每个小时的30分钟开始执行
      if( intMinute == iMinute && intSecond == iSecond )
      {
      Console.WriteLine("每个小时的30分钟开始执行一次!");
      }
     // 设置 每天的10:30:00开始执行程序
      if( intHour == iHour && intMinute == iMinute && intSecond == iSecond )
      {
      Console.WriteLine("在每天10点30分开始执行!");
      }
     }
    }
    }

    时间间隔执行这个objTimer_Elapsed()方法,即达到你要得效果

  • 相关阅读:
    路由器默认密码
    目前网络安全的攻击来源
    SQL注入——时间盲注
    UNIX网络编程第4章4.5listen函数4.6accept函数
    UNIX网络编程第3章套接字编程简介3.2套接字地址结构3.3值结果参数3.4字节排序函数
    Ubuntu软件系列---如何安装deb安装包
    Ubuntu软件系列---添加实时网速
    Ubuntu软件系列---网易云
    4.9 TF读入TFRecord
    4-8 使用tf.train.string_input_producer读取列表样本
  • 原文地址:https://www.cnblogs.com/soundcode/p/2525776.html
Copyright © 2011-2022 走看看