Asp.net 定时任务

1.定时器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Web.SessionState;
using System.Timers;

namespace WebApplication1
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            Timer time = new Timer();
            time.Interval = 1000;
            time.Enabled = true;
            time.AutoReset = true;
            time.Elapsed += new ElapsedEventHandler(TimeEvent);
        }
        private void TimeEvent(object source, ElapsedEventArgs e)
        {
            int intHour = e.SignalTime.Hour;
            int intMinute = e.SignalTime.Minute;
            int intSecond = e.SignalTime.Second;
            int iHour = 10;
            int iMinute = 30;
            int iSecond = 00;
            //每天10:30开始执行程序
            if (intHour == iHour && intMinute == iMinute && intSecond == iSecond)
            {
                  WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");
            } 
          
        }
        public void WriteStream(string content)
        {
            string path = "E:\\hong.txt";
            FileStream file = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(file);
            sw.Write(content);
            sw.Close();
        }
        
        void Application_End(object sender, EventArgs e)
        {
            //  在应用程序关闭时运行的代码

        }

        void Application_Error(object sender, EventArgs e)
        {
            // 在出现未处理的错误时运行的代码

        }

        void Session_Start(object sender, EventArgs e)
        {
            // 在新会话启动时运行的代码

        }

        void Session_End(object sender, EventArgs e)
        {
            // 在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
            // 或 SQLServer,则不会引发该事件。

        }

    }
}

2.cache

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Caching;

namespace WebApplication1
{
    public class Global : System.Web.HttpApplication
    {
        const string key="key";

        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码

            HttpRuntime.Cache.Insert(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
        }

        private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason)
        { 
            WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");
            HttpRuntime.Cache.Remove(key);
            HttpRuntime.Cache.Insert(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
        }
        public void WriteStream(string content)
        {
            string path = "E:\\hong.txt";
            FileStream file = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(file);
            sw.Write(content);
            sw.Close();
        }
        
        void Application_End(object sender, EventArgs e)
        {
            //  在应用程序关闭时运行的代码

        }

        void Application_Error(object sender, EventArgs e)
        {
            // 在出现未处理的错误时运行的代码

        }

        void Session_Start(object sender, EventArgs e)
        {
            // 在新会话启动时运行的代码

        }

        void Session_End(object sender, EventArgs e)
        {
            // 在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
            // 或 SQLServer,则不会引发该事件。

        }

    }
}

与想要的不一致

  private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason)
        { 
            HttpRuntime.Cache.Remove(key);
            HttpRuntime.Cache.Insert(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
            WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n"); 
        }

也不对,说明不是writeStream的问题

  void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            HttpRuntime.Cache.Add(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
        }

        private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason)
        { 
            HttpRuntime.Cache.Remove(key);
            HttpRuntime.Cache.Add(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
            WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n"); 
        }

还错

   const string key = "key";
        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration ,TimeSpan .FromSeconds (5), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
        }

        private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason)
        { 
            HttpRuntime.Cache.Remove(key);
            HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(5), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
            WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n"); 
        }

 

 const string key = "key";
        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration ,TimeSpan.FromMinutes (1), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
        }

        private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason)
        {
            if (HttpRuntime .Cache[key] != null)
            {
                HttpRuntime.Cache.Remove(key);
            }
            HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(1), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
            WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");
        }

发现很好

从以前的可以看出最小间隔时间是20s

  const string key = "key";
        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration ,TimeSpan.FromSeconds (20), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
        }

        private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason)
        {
            if (HttpRuntime .Cache[key] != null)
            {
                HttpRuntime.Cache.Remove(key);
            }
            HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(20), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
            WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");
        }
        public void WriteStream(string content)
        {
            string path = "E:\\hong.txt";
            FileStream file = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(file);
            sw.Write(content);
            sw.Close();
        }

好了

 3.threading

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Web.SessionState;
using System.Timers;
using System.Threading;

namespace WebApplication1
{
    public class Global : System.Web.HttpApplication
    {
        static AutoResetEvent wait = new AutoResetEvent(false);
        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            object state = new object();
            ThreadPool.RegisterWaitForSingleObject(wait, new WaitOrTimerCallback(test), state, 5000, false);

        }
        public void test(object state, bool timedOut)
        {
            WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");
        }
        public void WriteStream(string content)
        {
            string path = "E:\\hong.txt";
            FileStream file = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(file);
            sw.Write(content);
            sw.Close();
        }

        
        void Application_End(object sender, EventArgs e)
        {
            //  在应用程序关闭时运行的代码

        }

        void Application_Error(object sender, EventArgs e)
        {
            // 在出现未处理的错误时运行的代码

        }

        void Session_Start(object sender, EventArgs e)
        {
            // 在新会话启动时运行的代码

        }

        void Session_End(object sender, EventArgs e)
        {
            // 在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
            // 或 SQLServer,则不会引发该事件。

        }

    }
}

 static ManualResetEvent wait = new ManualResetEvent(false);        
        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            object state = new object();
            ThreadPool.RegisterWaitForSingleObject(wait, new WaitOrTimerCallback(test), state, 5000, false);
        }

        public void test(object state, bool timedOut)
        {
            WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");
        }
        public void WriteStream(string content)
        {
            string path = "E:\\hong.txt";
            FileStream file = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(file);
            sw.Write(content);
            sw.Close();
        }

 http://www.cnblogs.com/Kazaf/archive/2012/03/26/2417341.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值