定时执行某段程序,在网上查了相关资料,基本上都说用全局应用程序"Global.asax"
我写了一段自动生成XML的代码,弄好之后运行,系统"string url = Server.MapPath("zzzz/Company.xml");
"报错,说服务器操作在此上下文中不可用。
请问大家,该怎么办?
主要代码贴上:<%@ Application Language="C#" %>   
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Timers" %>
<%@ import Namespace="System.Threading" %>
<%@ import Namespace="System.Xml" %>
<%@ import Namespace="System.Data" %>
  
<script runat="server">
    
    public class Time_Task
    {
        public event System.Timers.ElapsedEventHandler ExecuteTask;        private static readonly Time_Task _task = null;
        private System.Timers.Timer _timer = null;
        private int _interval = 1000;        public int Interval
        {
            set
            {
                _interval = value;
            }
            get
            {
                return _interval;
            }
        }        static Time_Task()
        {
            _task = new Time_Task();
        }        public static Time_Task Instance()
        {
            return _task;
        }        public void Start()
        {
            if (_timer == null)
            {
                _timer = new System.Timers.Timer(_interval);
                _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
                _timer.Enabled = true;
                _timer.Start();
            }
        }        protected void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (null != ExecuteTask)
            {
                ExecuteTask(sender, e);
            }
        }        public void Stop()
        {
            if (_timer != null)
            {
                _timer.Stop();
                _timer.Dispose();
                _timer = null;
            }
        }
    }
    
    void Application_Start(object sender, EventArgs e)
    {
        Time_Task.Instance().ExecuteTask += new System.Timers.ElapsedEventHandler(Global_ExecuteTask);
        Time_Task.Instance().Interval = 1000 * 5;//表示间隔1分钟
        Time_Task.Instance().Start();
    }    void Global_ExecuteTask(object sender, System.Timers.ElapsedEventArgs e)
    {
        //在这里编写需要定时执行的逻辑代码        string url = Server.MapPath("zzzz/Company.xml");//<--------------出错地方!!
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(url);
        //加载XML文档
        XmlNode root = xmlDoc.SelectSingleNode("ORGANIZATIONS");//查找第一个匹配的XmlNode
        ……     }
       
    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)    
    {   
        // 在会话结束时运行的代码。    
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为   
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer    
        // 或 SQLServer,则不会引发该事件。   
  
    }
</script>  

解决方案 »

  1.   

    做成WINFORM程序或则WINDOWS服务把..
      

  2.   

    因为是自动运行服务,所以没有Page,HttpContent等连接相关的变量,Server同样无法访问,你的目录需要通过其他方式配置,比如配置到Web.Config文件。
      

  3.   

    "string url = Server.MapPath("zzzz/Company.xml"); 
    这个资源路径是不是有问题。如果是解决方案里有的话应该是Server.MapPath("~/zzzz/Company.xml")吧 
      

  4.   

    用AppDomain.CurrentDomain.BaseDirectory +"\\zzzz\\Company.xml",换掉Server.MapPath,看下路径是不是能取到
      

  5.   

    感谢6楼,此方法可行,问题已解决
    4楼提供了一个或许可行的方向,能不能稍微详细点教教我,webconfig我一直不懂配置
    1小时后结贴,无新回复4楼10分,6楼90
      

  6.   

    不是,你现在定时服务等于是IIS在帮你执行,用用就不行了,最好是做成windows service
    创建一个windows service项目
    在onstart里写一个while循环
    while(true)
    {
    //另开个线程执行任务
    //
    thread.sleep(60000);//时间自己设
    }