定时执行一段代码
Global.asax void Refresh()
    {
        while (true)
        {
                System.Web.HttpContext current = System.Web.HttpContext.Current;
                if (current != null)
                {
                   System.Web.HttpContext.Current.Application..........
                }
                Thread.CurrentThread.Join(1000 * 60 * 1);//阻止 分钟  
        }
    }
    
     void Application_Start(object sender, EventArgs e) 
    {
        //在应用程序启动时运行的代码         
        thread = new Thread(new ThreadStart(Refresh));        
        thread.Name = "更新特价数据";        
        thread.Start();
    }
执行的代码中需要用到 Application,但System.Web.HttpContext.Current永远为空,除了在Global.asax中定时执行外,还有什么方法?

解决方案 »

  1.   

    多线程执行,上下文信息将丢失,因此System.Web.HttpContext.Current=null,因此多线程下不要使用上下文。可以借助客户端的js定时进行执行。
      

  2.   

    定时执行,那就是说明,没有人请求你,既然没有人请求你,又何来的请求上下文?不过你要访问Application应该是不需要访问请求的上下文环境吧
      

  3.   

    void Application_Start(object sender, EventArgs e)  
      {
      Application[""]="";
     //在   Refresh中使用
      thread = new Thread(new ThreadStart(Refresh));   
      thread.Name = "";   
      thread.Start();
      }
    Application_Start 调用时,Request 没有创建
      

  4.   

    我不喜欢这种方式的定时执行,如果是自己的服务器,可以写一个winform实用程序,如果不是自己的服务器,没权限,那就麻烦了
      

  5.   

    "我不喜欢这种方式的定时执行,如果是自己的服务器,可以写一个winform实用程序,如果不是自己的服务器,没权限,那就麻烦了"是自己的服务器,我已经写了个windowds服务程序,定时更新数据库的数据,但页面展示的是:第一次加载时,数据存到Application中,以后都是从Application中获取,我想定时更新Application中的数据,还有什么办法?
      

  6.   

      System.Web.HttpContext current = System.Web.HttpContext.Current;
    这个最好定义在while外.