是否需要在global里,做一个timer啊。
这是生成静态的方法
public void Dohtml(string Ourl, string Path)
我在global调用:
 
BLL.BLL hh = new BLL.BLL();
hh.Dohtml("http://localhost/default.aspx", System.Web.HttpContext.Current.Server.MapPath("./"));但是不成功。在百度,google里搜索了无数次,仍为找到答案。请高手赐教。
-------------------------------------------------------------我global里的代码是这样的:
protected void Application_Start(object sender, EventArgs e)
        {            // 在应用程序启动时运行的代码
            System.Timers.Timer myTimer = new System.Timers.Timer(1000 * 1 * 1);
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);//时间到了执行
            myTimer.Interval = 1000 * 1 * 1;
            myTimer.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件
            myTimer.AutoReset = true;
          //  t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); 
        }
        private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {
 
            try
            {
                BLL.BLL hh = new BLL.BLL();
                hh.Dohtml("http://localhost/hengshuirc/default.aspx", System.Web.HttpContext.Current.Server.MapPath("./"));
            }
            catch { }
        }

解决方案 »

  1.   

    这个问题我也遇到过,当初自己是从网上档的代码,对多线程缺乏认知。
    楼主应该知道在托管环境下如果你的前台线程执行完后,那么依赖前台线程的后台线程也会自动被abort掉,除非你在主线程中阻塞一下,等待后台线程的完成。
    最好把上下文获取本地路径放到主线程中,然后作为参数传到从线程中,以下是简单的调用,这里如果参数太多或是复杂参数,你可以构造一个线程辅助类,将所有的参数放到辅助对象中,作为state的参数
     protected void Application_Start(object sender, EventArgs e)
            {
                HttpContext context = HttpContext.Current;
                string mappath = context.Server.MapPath("static");
                Timer time = new Timer(new TimerCallback(CreateToStaticHtml), mappath, 1000, 5000);
            }
            private void CreateToStaticHtml(object state)
            {
                FileStream fs = new FileStream(string.Format("{0}/{1}.html",state.ToString(), DateTime.Now.ToString().Replace("-","").Replace(":","").Replace(" ","")),FileMode.OpenOrCreate, FileAccess.Write,FileShare.Read);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write("dfghjkl;dfghjkl");
                sw.Close();
            }