Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# windows服务中未调用计时器勾选事件_C# - Fatal编程技术网

C# windows服务中未调用计时器勾选事件

C# windows服务中未调用计时器勾选事件,c#,C#,我有一个窗口服务,它在指定的时间播放声音文件,所以我使用了计时器,但它的滴答声事件从未触发,这在WinForm应用程序中也起作用 下面是我的服务的代码片段 public partial class ClockService : ServiceBase { private TimeSpan _alarmTime = new TimeSpan(9, 55, 0); private int _snoozeTime = 2; // 2 minutes private int _r

我有一个窗口服务,它在指定的时间播放声音文件,所以我使用了计时器,但它的滴答声事件从未触发,这在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";
    }

请任何人看一下并指导我…问题是滴答声事件从未触发。

尝试使用
系统.Timers.Timer

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

System.Windows.Forms.Timer
(顾名思义)可以在表单应用程序中工作,但不能在NT服务中工作。

不要使用System.Windows.Forms.Timer,使用System.Threading.Timer


服务不是一种形式。

我认为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);

尝试使用其他计时器:)

您确定问题出在计时器上吗?windows服务访问声音系统是极不寻常的,(您将如何关闭它?)

话虽如此,TomTom是对的,表单计时器是关于能够封送到UI线程的,而您没有,而是使用线程计时器