window 服务中添加定时器

来源:互联网 发布:api.ai 新域名 编辑:程序博客网 时间:2024/05/17 17:16
这几日因为要写一个window服务定时的更新数据,以前没有涉及到这个方面。建立好服务之后直接从工具栏中拖了一耳光timer,但是将服务安装到本地测试的是一直都不执行代码,后来发现拖进的timer是属于windows.form的控件,好像是服务不支持这个吧。现在就改成System.Timers.Timer。
 protected override void OnStart(string[] args)        {            System.Timers.Timer time1 = new System.Timers.Timer(1000);            time1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);            time1.AutoReset = true;            time1.Enabled = true;        }
这样就可以执行了。
原创粉丝点击