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
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28