在Windows服务中不调用计时器tick事件

时间:2010-12-30 09:31:50

标签: c#

我有一个窗口服务,它在指定的时间播放声音文件,所以为了做到这一点,我已经使用了Timer,但它的Tick事件永远不会被触发,同样适用于WinForm应用程序。

以下是我服务的代码段...

public partial class ClockService : ServiceBase
{
    private TimeSpan _alarmTime = new TimeSpan(9, 55, 0);
    private int _snoozeTime = 2; // 2 minutes
    private int _repeatCounter = -1;
    private const int ALARM_REPETITION = 5;
    private string _alarmSoundPath = @"C:\Sound\default.wav";
    private string _alarmLogPath = @"C:\Sound\log.txt";

    public ClockService()
    {
        InitializeComponent();
        alarmTimer.Enabled = true;
    }

    protected override void OnStart(string[] args)
    {
        using (FileStream fileStream = new FileStream(_alarmLogPath, FileMode.OpenOrCreate))
        {
            using (StreamWriter writer = new StreamWriter(fileStream))
            {
                writer.WriteLine("Service has started at {0}", DateTime.Now);
            }
        }
    }

    protected override void OnStop()
    {
        using (FileStream fileStream = new FileStream(_alarmLogPath, FileMode.OpenOrCreate))
        {
            using (StreamWriter writer = new StreamWriter(fileStream))
            {
                writer.WriteLine("Service has stopped at {0}", DateTime.Now);
            }
        }
    }

    private void alarmTimer_Tick(object sender, EventArgs e)
    {
        using (FileStream fileStream = new FileStream(_alarmLogPath, FileMode.OpenOrCreate))
        {
            using (StreamWriter writer = new StreamWriter(fileStream))
            {
                writer.WriteLine("Alarm time is: {0}", _alarmTime);
            }
        }

        TimeSpan currentTime = new TimeSpan(DateTime.Now.TimeOfDay.Hours, DateTime.Now.TimeOfDay.Minutes, DateTime.Now.TimeOfDay.Seconds);


        if (currentTime == _alarmTime)
        {
            using (FileStream fileStream = new FileStream(_alarmLogPath, FileMode.OpenOrCreate))
            {
                using (StreamWriter writer = new StreamWriter(fileStream))
                {
                    writer.WriteLine("Alarm time is: {0} and current time is: {1}", _alarmTime, currentTime);
                }
            }

            _alarmTime = _alarmTime.Add(new TimeSpan(0, _snoozeTime, 0));
            _repeatCounter++;

            if (File.Exists(_alarmSoundPath))
            {
                SoundPlayer player = new SoundPlayer(_alarmSoundPath);
                player.PlayLooping();
            }

        }

        if (_repeatCounter == ALARM_REPETITION)
        {
            _alarmTime = _alarmTime.Subtract(new TimeSpan(0, (ALARM_REPETITION * _snoozeTime), 0));
            _repeatCounter = 0;
        }

    }
}

为确保我已正确完成所有操作,这是我的InitializeComponent方法

    private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                this.alarmTimer = new System.Windows.Forms.Timer(this.components);
                this.alarmTimer.Enabled = true;
                this.alarmTimer.Interval = 1000; 
            this.alarmTimer.Tick += new System.EventHandler(this.alarmTimer_Tick);
        this.ServiceName = "PravoAlarmService";
    }

请有人看看并指导我......问题是嘀嗒事件永远不会被解雇..

5 个答案:

答案 0 :(得分:10)

尝试使用System.Timers.Timer代替。

this.alarmTimer = new System.Timers.Timer();

System.Windows.Forms.Timer - 正如其名称所暗示的 - 适用于表单应用程序,但适用于NT服务。

答案 1 :(得分:1)

我认为System.Timers.Timer是一个更好的选择:

Timer _timer = new Timer();
// In miliseconds 60000 = 1 minute
// This timer will tick every 1 minute
_timer.Interval += 6000;
// Activate the timer
_timer.Enabled = true;
// When timer "tick"
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);

答案 2 :(得分:0)

不要使用System.Windows.FOrms.Timer,请使用System.Threading.Timer。

服务不是表格。

答案 3 :(得分:0)

尝试使用其他Timer :) here is difference

答案 4 :(得分:0)

你确定这是问题的定时器吗? Windows服务访问音响系统是非常不寻常的(如何将其关闭?)它可能被阻止,或者可能需要“与桌面交互”。

话虽如此,TomTom是对的,Forms Timers是关于能够使用你没有的UI线程,而不是使用线程计时器。