在Windows服务中创建计时器 [英] creating a timer in windows service

查看:78
本文介绍了在Windows服务中创建计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Windows Service应用程序中创建了一个计时器,它应该每1秒执行一次方法,但是我的问题是计时器可以工作几秒钟,然后自动停止
服务应在文本中写一行,该行包含Windows日期和时间,并且Windows时间应每1秒更改一次
如果计时器停止或整个服务停止,我不知道
我需要有人解释为什么会发生这种情况

i have created a timer in windows service application and it should execute a method every 1 sec but my problem is the timer works for several seconds and then stop automatically
the service should write a line in the text, that line contains the windows date and time,and the windows time should change every 1 sec
i dunno if the timer is stopped or the whole service is stopped
i need someone to explain why is this happening

protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            System.Timers.Timer T1 = new System.Timers.Timer();
            T1.Interval = (1000);
            T1.AutoReset = true;
            T1.Enabled = true;
            T1.Start();
            T1.Elapsed += new System.Timers.ElapsedEventHandler(T1_Elapsed);
        }
 
        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            System.Timers.Timer T1 = new System.Timers.Timer();
            T1.Interval = (1000);
            T1.AutoReset = true;
            T1.Enabled = true;
            T1.Start();
            T1.Elapsed += new System.Timers.ElapsedEventHandler(T1_Elapsed);
        }
        private void T1_Elapsed(object sender, EventArgs e)
        {
            FileStream TS = new FileStream(@"C:\Test.txt", FileMode.OpenOrCreate);
            string lines = "Service has been started at" + DateTime.Now.ToString();
            System.IO.StreamWriter file = new System.IO.StreamWriter(TS);
            file.WriteLine(lines);
            file.Close();
            TS.Close();
        }

推荐答案

这是一个糟糕的设计.

0)为什么不为此使用系统日志?

1)为什么会引起这么多的文件I/O?该系统将花费大量时间写磁盘,以致没有时间去做别的事情.如果绝对需要每秒写入磁盘,则将要写入的文本缓冲一两分钟,然后一次将缓冲区刷新到文件中.

2)如果您的文本文件设法填满磁盘会发生什么?

3)问题可能是您正在尝试处理计时器事件,而最后一个事件仍在处理中(请参阅上面的项目1).

4)在T1_Elapsed方法中的代码周围放置一个try catch块,如果抛出异常,该代码将简单地忽略该事件.

That''s a terrible design.

0) Why don''t you use the system log for that?

1) Why are you causing so much file I/O? the system is going to be spending so much time writing to disk that it won''t have much time for anything else. If you absolutely need to write to the disk every second, buffer the text to be written for a minute or two, and then flush the buffer to the file all at once.

2) What happens if your text file manages to fill the disk?

3) The problem is probably that you''re trying to handle the timer event while the last time event is still being processed (see item 1 above).

4) Put a try catch block around the code in the T1_Elapsed method that will simply ignore the event if an exception is thrown.

try
{
   do my processing
}
catch (Exception)
{
}


要在启动和停止服务时都创建计时器实例吗?

您应该在init方法中创建计时器,构造函数就足够了,然后在OnStart事件处理程序中启动它,然后在onStop事件处理程序中停止它.

另外,打开和关闭FileStream对象是一个很费力的过程,每秒执行一次操作不会使您的服务表现出色.

您是否检查过Windows事件日志中有关您服务的任何消息?
You are creating a timer instance both when the service is started and stopped?

You should create the timer in an init method, the constructor would suffice, then start it in the OnStart event handler and stop it in the onStop event handler.

Also, opening and closing FileStream objects is an intensive process, doing it every second won''t make your service very performant.

Have you checked the Windows event log for any messages about your service?


这是一个糟糕的设计.

避免使用计时器.当计时器造成的危害大于帮助时,这是完全正确的情况.
而是创建一个将重复调用的线程,并使用System.Threading.Thread.Sleep-所需的时间.

确实使用日志记录.

—SA
That''s a terrible design.

Avoid using timers. This is exactly a case when a timers can make a lot more harm than help.
Instead, create a thread which will repeat the call, use System.Threading.Thread.Sleep — for required amount of time.

Use logging indeed.

—SA


这篇关于在Windows服务中创建计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆