• asp.net或者MVC定时自动执行某操作


    using System;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Text;
    
    namespace SystemTask
    {
        public class CensusdemoTask
        {
            System.Threading.Timer timer;
            private static int count = 1;
    
            public CensusdemoTask()
            {
                //3秒执行一次
                timer = new System.Threading.Timer(SetCensusURL, null, 0, 1000 * 3);
            }
    
    
            [MethodImpl(MethodImplOptions.Synchronized)]
            public void SetCensusURL(object obj)
            {
                var date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string txt = string.Empty;
                if (count==1)
                {
                    txt += string.Format("========系统重启========
    ");
                }
                txt += string.Format("写入时间:{0},次数{1}", date, count);
                FileStream fs = null;
                StreamWriter sw = null;
                try
                {
                    string path = "D:\1.txt";//文件的路径,保证文件存在。
                    fs = new FileStream(path, FileMode.Append);
                    sw = new StreamWriter(fs);
                    sw.WriteLine(txt);
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    sw.Dispose();
                    sw.Close();
                    fs.Dispose();
                    fs.Close();
                }
                count++;
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Caching;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace Mvc
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                RouteConfig.RegisterRoutes(RouteTable.Routes);
    
                //在Global.asax的Application_Start中注册任务
                SystemTask.CensusdemoTask t = new SystemTask.CensusdemoTask();
            }
        }
    }


  • 相关阅读:
    Redis设计与实现第一部分:第5章:Redis 跳跃表
    根据临时表修改主表的某字段数据根据主表的主键
    Redis设计与实现第一部分:第2章:简单动态字符串SDS
    Redis
    MySQL的访问控制与用户管理
    MySQL字符集和语言的基础知识
    生成日志文件
    Python进阶09 动态类型
    Python进阶08 异常处理
    Python进阶07 函数对象
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234033.html
Copyright © 2020-2023  润新知