//全局配置文件中
protected void Application_Start()
{
//定时任务
System.Timers.Timer timer = new System.Timers.Timer(10000);
//绑定方法
timer.Elapsed += new ElapsedEventHandler(AddCount);
//AddCount是一个方法,此方法就是每个1秒而做的事情
timer.AutoReset = true;
timer.Enabled = true;
}
/// <summary>
/// 处理事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AddCount(object sender, ElapsedEventArgs e)
{
//这里编写你需要执行的代码
//int intHour = e.SignalTime.Hour;
//int intMinute = e.SignalTime.Minute;
//int intSecond = e.SignalTime.Second;
DateTime now = DateTime.Now;
DateTime firstDay = new DateTime(now.Year, now.Month, 1);
DateTime lastDat = firstDay.AddMonths(1).AddDays(-1);
}
/// <summary>
/// Application 结束的时候,防止IIS程序池自动收回问题
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Application_End(object sender, EventArgs e)
{
//下面的代码是关键,可解决IIS应用程序池自动回收的问题
Thread.Sleep(1000);
//这里设置你的web地址,可以随便指向你的任意一个aspx页面甚至不存在的页面,目的是要激发Application_Start
string url = "http://www.shaoqun.com";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流
}