+ -
当前位置:首页 → 问答吧 → C# 的timer 问题 为什么同样的timer 只执行了一个(本人菜鸟)

C# 的timer 问题 为什么同样的timer 只执行了一个(本人菜鸟)

时间:2011-12-19

来源:互联网

C# code

//此处引用
 static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new CollDataService() 
            };
            ServiceBase.Run(ServicesToRun);
        }
    }


//
 public partial class CollDataService : ServiceBase
    {
        //声明数据采集定时器
        private Timer tmrCollectTimer;//这个timer可以执行 达到预计效果

        private Timer autoClearLogTimer;//这个失败
        public CollDataService()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 启动服务时,从配置文件中读取采集时间间隔
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            try
            {

                if (autoClearLogTimer == null)
                {
                    autoClearLogTimer = new Timer();
                    autoClearLogTimer.Interval = (double)(1 * 60 * 1000);
                    autoClearLogTimer.Elapsed += new ElapsedEventHandler(autoClearLogTimer_Elapsed);

                }
                autoClearLogTimer.Enabled = true;
                autoClearLogTimer.Start();

            }
            catch
            { 
            
            }



            try
            {
                //配置时以秒为单位,在设置时要转换为Ms
                long intervalTime = int.Parse(SystemConfigure.GetConfigureValue("CollectDataInterval")) * 1000;
                if (tmrCollectTimer == null)
                {
                    tmrCollectTimer = new Timer();
                    tmrCollectTimer.Interval = (double)intervalTime;
                    tmrCollectTimer.Elapsed += new ElapsedEventHandler(tmrCollectTimer_Elapsed);

                }               
                Log.WriteLog(LogType.OperateLog, DateTime.Now.ToString() + " 启动数据采集服务");
                tmrCollectTimer.Enabled = true;
                tmrCollectTimer.Start();

            }
            catch (Exception ex)
            {
                Log.WriteLog(LogType.ErrorLog, DateTime.Now.ToString() + " 启动服务发生异常:"+ex.Message);
            }


          
        }


void autoClearLogTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
           // 此处为要执行的代码 //              
        }


 void tmrCollectTimer_Elapsed(object sender, ElapsedEventArgs e)
        {            
           // 此处为要执行的代码 //              
        }

 protected override void OnStop()
        {
            if (tmrCollectTimer != null)
            {
                tmrCollectTimer.Enabled = false;
                tmrCollectTimer.Stop(); 
            }

            if (autoClearLogTimer != null)
            {
                autoClearLogTimer.Enabled = false;
                autoClearLogTimer.Stop();
            }
        }



作者: liu845732363   发布时间: 2011-12-19

改不同的命名试试

作者: ywdfei   发布时间: 2011-12-19

引用 1 楼 ywdfei 的回复:
改不同的命名试试


请问具体改哪个地方呢 本来只有tmrCollectTimer 一个timer的 autocleartimer 是我按这形式添加上去的 觉得应该能实现功能,结果不可以。。

作者: liu845732363   发布时间: 2011-12-19

热门下载

更多