Timer tick event is not called in windows service

后端 未结 5 1333
梦如初夏
梦如初夏 2021-01-06 13:29

I have a window service which plays a sound file at a specified time, so to do that I have taken Timer but it\'s Tick event is never fired, same is working in WinForm applic

相关标签:
5条回答
  • 2021-01-06 14:02

    try to use other Timer :) here is difference

    0 讨论(0)
  • 2021-01-06 14:03

    Try to use the System.Timers.Timer instead.

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

    The System.Windows.Forms.Timer - as it names implies - works in Forms applications, but not in something like a NT Service.

    0 讨论(0)
  • 2021-01-06 14:16

    I think System.Timers.Timer is a better choice :

    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);
    
    0 讨论(0)
  • 2021-01-06 14:16

    Dont use a System.Windows.FOrms.Timer, use a System.Threading.Timer.

    A service is not a form.

    0 讨论(0)
  • 2021-01-06 14:16

    Are you sure it's the timer that's the issue? It's extremely unusual for a windows service to access the sound system, (How would you turn it off?) it may be blocked or it may require "interaction with desktop" enabled.

    Having said that, TomTom is right, Forms Timers are about being able to Marshal to the UI thread, which you don't have, rather using a Threading timer.

    0 讨论(0)
提交回复
热议问题