简体   繁体   中英

Windows Service Tick ElapsedEventArgs is never called

I tried to deploy one service which works great in local to the production server. But on this server, the ElapsedEventHandler seems to never call my method Tick()

I have read other similar threads as First Windows Service - timer seems not to tick or Timer tick event is not called in windows service but i couldn't have any answer to my problem :/

I also tried to use _timer.Enabled=1 instead of _timer.Start (no incidence normally but still tried) and also tried to reinstall service a couple of times.

I use the Timer from Class System.Timers. here is my code :

static void Main()
{
    var servicesToRun = new ServiceBase[] 
    { 
        new SynchronizeEvents() 
    };
    #if DEBUG
    servicesToRun.LoadServices();
    #else
    ServiceBase.Run(servicesToRun);
    #endif
}

and

public partial class SynchronizeEvents : ServiceBase
{
    private readonly Logger _logger = LogManager.GetCurrentClassLogger();//Nlog
    private Timer _timer;//From Class System.Timers

    private readonly DbSetName _db = new DbSetName ();

    private static readonly Dictionary<Employee, otherStuff> Subscriptions = new Dictionary<Employee, otherStuff>();
    public SynchronizeEvents()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        _logger.Trace("Service Start");
        _timer = new Timer(5000) { AutoReset = false };//Same problem without AutoReset = false
        _timer.Elapsed += Tick;
        _timer.Start();
        _logger.Trace("End OnStart");
    }

    protected override void OnShutdown()
    {
        _logger.Trace("OnShutdown");
        _timer.Stop();
        base.OnShutdown();
    }

    protected void Tick(object sender, ElapsedEventArgs elapsedEventArgs)
    {
        _logger.Trace("Tick");
        _timer.Stop();
        Compute();
        _timer.Start();
    }
    protected override void OnStop()
    {
        _logger.Trace("OnStop");
    }

Everything works great while on DebugMode and also on local computer in Release mode, but when i install it on production server the only logs i got are :

2014-09-08 16:35:57.1929 TRACE Service Start

2014-09-08 16:35:57.2085 TRACE End OnStart

i also got this when i stop the service...

2014-09-08 16:40:52.4072 TRACE End OnStop

Actually i got to apologize to Steve Wellens, he was right to ask about what Compute do. The problem was related to Compute(). It had some code which required a DLL which was missing. I don't know why _logger.Trace("Tick"); wasn't called and no Exception was triggered :/ I had to comment my program line per line to find out where the problem came from...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM