asp.net中定时执行某个方法

来源:互联网 发布:php算法面试题及答案 编辑:程序博客网 时间:2024/06/08 04:33

        using System.Threading;
        using System.Collections;
        using System.IO;
        using System.Text; 

      string LogPath;
        Thread thread;
        protected void Page_Load(object sender, EventArgs e)
        {
            LogPath = HttpContext.Current.Server.MapPath("log.txt");
            //在应用程序启动时运行的代码
            thread = new Thread(new ThreadStart(WriteLog));   //方法没有();
            thread.Name = "写登录日志线程";
            thread.Start();
        }
        //方法前面不加修饰词
        void WriteLog()
        {
            while (true)
            {
                StreamWriter sw = new StreamWriter(LogPath, true, Encoding.UTF8);
                sw.WriteLine(thread.Name + ":" + DateTime.Now.ToString());
                sw.Close();
                Thread.CurrentThread.Join(1000 * 60);//阻止1分钟
            }
        }

原创粉丝点击