日期:2014-05-17  浏览次数:20432 次

全局类计划任务
我写了一个定时任务
在全局类文件中代码如下
C# code

void Application_Start(object sender, EventArgs e)
    {
        //在应用程序启动时运行的代码
        WebSiteRootPath = Server.MapPath("~");
        System.IO.File.AppendAllText(WebSiteRootPath + "\\log.txt", "Application_Start 开始:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine);

   
        System.Timers.Timer myTimer = new System.Timers.Timer(60000);          
        myTimer.Elapsed += new System.Timers.ElapsedEventHandler(doJob);
        myTimer.Enabled = true;
        myTimer.AutoReset = true;
    }

    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)
    {
         //用来监测程序池回收
        System.IO.File.AppendAllText(WebSiteRootPath + "\\log.txt", "Application_Ent 会话结束:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine);
        //在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式 
        //设置为 StateServer 或 SQLServer,则不会引发该事件。
        System.Threading.Thread.Sleep(1000);
        //这里设置你的web地址,可以随便指向你的任意一个aspx页面甚至不存在的页面,目的是要激发Application_Start      
        //使用您自己的URL     
        string url = "http://www.云阳面坊.com/";
        System.Net.HttpWebRequest myHttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
        System.Net.HttpWebResponse myHttpWebResponse = (System.Net.HttpWebResponse)myHttpWebRequest.GetResponse();
        System.IO.Stream receiveStream = myHttpWebResponse.GetResponseStream();
    }



并且设置了sessionstate 模式,配置文件代码为:
XML code

  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
    <sessionState mode="InProc" />
    </system.web>
</configuration>


但是定时任务并没有按照预期的任务,数据更新了一部分后就没有更新了

用来监测程序回收问题的文件内容为:
Application_Start 开始:2012-04-07 09:02:25
Application_Ent 会话结束:2012-04-07 09:22:40
Application_Ent 会话结束:2012-04-07 09:55:40
Application_Ent 会话结束:2012-04-07 09:59:20
Application_Start 开始:2012-04-07 10:21:55

我认为问题出在 会话结束后没有激发页面


请各位高手帮忙看看
原因是什么

是哪里出错了吗?

------解决方案--------------------
嗯,把这个“重启”也许说成是“停止”在你这个问题里更合适。没有客户端请求激发页面,asp.net应用是不会自动启动的。
------解决方案--------------------
计划任务还是写成Windows服务比较好,而不是写在ASP.NET的全局代码中。