我使用System.Timers来安排任务。我觉得这很有效。但是我不太确定这样做是否可行。
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Timers;
using System.IO;
using System.Threading;
namespace WebApplication2
{
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 = 1000;
timer1.Elapsed += timer1_Elapsed;
}
void timer1_Elapsed(object sender, ElapsedEventArgs e)
{
timer1.Stop();
Thread.Sleep(4000);
// Do something. Example:
File.AppendAllText("C:\\log.txt", DateTime.Now + "\r\n");
}
}
}
通过使用此方法,我可以通过调用此方法在应用程序的任何位置启动任务:
WebApplication2.Global.timer1.Start();
您怎么看?