可以通过在ASP.NET中使用静态全局计时器来调度任务吗? [英] Can Task Be Scheduled by using static global Timers in ASP.NET?

查看:79
本文介绍了可以通过在ASP.NET中使用静态全局计时器来调度任务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 System.Timers 来安排任务。我发现这个工作正常,但我不太确定这样做是否可以。



I'm using System.Timers to schedule tasks. I find this working, but I'm not quite sure if it's ok to do it this way.

using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Timers;
using System.IO;
using System.Threading;

namespace WebApplication1
{
    public class Global : System.Web.HttpApplication
    {
        public static System.Timers.Timer timer1 = new System.Timers.Timer();

        protected void Application_Start(object sender, EventArgs e)
        {
            timer1.Interval = 100;
            timer1.Elapsed += timer1_Elapsed;
        }

        void timer1_Elapsed(object sender, ElapsedEventArgs e)
        {
            timer1.Stop();
            // Set the time here
            // Example: 6 hours
            Thread.Sleep(6 * 60 * 60 * 1000);
            // Do something. Example:
            File.AppendAllText("C:\\log.txt", DateTime.Now + "\r\n");
        }
    }
}



通过使用此方法,我可以通过调用此方法在应用程序中的任何位置安排任务:




By using this method, I can schedule the task anywhere in the application by calling this:

WebApplication1.Global.timer1.Start();





你怎么看?



What do you think?

推荐答案

不,不是真的。 IIS有一个停止你的应用程序的习惯,如果它在很长一段时间内没有活动。



在所涉及的时间范围内,这将更好地设置为单独的可执行文件的计划任务。
No, not really. IIS has a habit of stopping your application if it hasn't been active in quite a while.

Do to the time frame involved, this would be much better setup as a Scheduled Task of a separate executable.


这篇关于可以通过在ASP.NET中使用静态全局计时器来调度任务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆