Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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:

C#
WebApplication1.Global.timer1.Start();


What do you think?
Posted
Updated 10-Jun-14 15:35pm
v2

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900