代码如下:
  public FileResetService()
        {
            InitializeComponent();
            TimeDo();
        }        protected override void OnStart(string[] args)
        {
            //TimeDo();
            
        }        /// <summary>
        /// /重新启动
        /// </summary>
        public void Reset()
        {
            //关闭进程
            System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("FileResultColection");
            foreach (System.Diagnostics.Process p in process)
            {
                p.Kill();
            }
            //启动程序
            System.Diagnostics.Process.Start(@"F:\Work\快分系统\VS2008新仪器数据采集\DataAcquisitionSolution\FileResultColection\bin\FileResultColection.exe");
        }
        public void TimeDo()
        {
            //System.Timers.Timer aTimer = new System.Timers.Timer();
            //aTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimeEvent);
            timer1.Enabled = true;
            timer1.Interval = 1000;
        }
        ///// <summary>   
        ///// 定时器触发事件   
        ///// </summary>   
        ///// <param name="source"></param>   
        ///// <param name="e"></param>   
        //private void TimeEvent(object source, System.Timers.ElapsedEventArgs e)
        //{
           
        //}
        public  void WirteLogWithName(string msg, string filePath)
        {
            for (int i = 0; i < 5; i++)
            {
                try
                {
                    System.IO.File.AppendAllText(filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Info>" + msg + System.Environment.NewLine, System.Text.Encoding.UTF8);
                    break;
                }
                catch (System.Exception e)
                {
                    System.Threading.Thread.Sleep(500);
                    continue;
                }
            }
        }
        protected override void OnStop()
        {
            
        }        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            int intHour = e.SignalTime.Hour;
            int intMinute = e.SignalTime.Minute;
            int intSecond = e.SignalTime.Second;
            int iHour = 12;
            int iMinute = 47;
            int iSecond = 00;
            // 设置重启启动的的时间  
            if (intHour == iHour && intMinute == iMinute && intSecond == iSecond)
            {
                Reset();            }
            iMinute = 48;
            if (intHour == iHour && intMinute == iMinute && intSecond == iSecond)
            {
                Reset();
            }
            iMinute = 49;
            if (intHour == iHour && intMinute == iMinute && intSecond == iSecond)
            {
                Reset();
            }
            WirteLogWithName("重启启动**********************", Environment.CurrentDirectory.ToString() + @"\log\" + "ResetLog.log");
        }
    }
}在winform程序里代码没有问题 可以触发。。但是服务里就是不行。。无法重启。。不知道代码问题出在哪里,求教大人指点。。服务应该是装好了的。我的电脑服务里面也有并且已启动,进程里也有,但就是无法触发我的代码。。高人指点啊最近少上论坛 ,没什么分,但还是请大家多多指教哈,小弟感激不尽

解决方案 »

  1.   

    timer1.Enabled = true;
    timer1.Interval = 1000;换个位置
      

  2.   

    事件没有触发,就是因为事件没有注册。
    timer组件的Event页上,Elapsed事件有内容吗?
      

  3.   

    那两句换个位置
    timer1.Interval = 1000;
    timer1.Enabled = true;
      

  4.   

    从结果找原因
    1. timer1.Enabled = false;
    2. 在需要触发事件的位置注册事件断点调试,看看到底在哪儿出问题了。
      

  5.   

    在winform程序里代码没有问题 可以触发。。但是服务里就是不行。。无法重启
    很明显了 权限问题 不存在代码问题
      

  6.   

    你这个服务装上了没有,如果装上了,也能启动的话,就是你的操作系统有问题了,请问你的操作系统是不是Windows7? 应该是吧。 现在Windows7中不允许再这样做了。
     //启动程序
                System.Diagnostics.Process.Start(@"F:\Work\快分系统\VS2008新仪器数据采集\DataAcquisitionSolution\FileResultColection\bin\FileResultColection.exe");