will
will

Reputation: 897

Why won't my timer event kickoff?

I have a windows service with a class level timer. The Elapsed event call this Work method:

protected override void Work()
    {
        if (!File.Exists(@"C:\Users\Will\Desktop\Developer stuff\junk text files\reusableservicetest.txt"))
            File.Create(@"C:\Users\Will\Desktop\Developer stuff\junk text files\reusableservicetest.txt");
        File.WriteAllLines(@"C:\Users\Will\Desktop\Developer stuff\junk text files\reusableservicetest.txt",
            new[] { DateTime.Now.ToLongTimeString() });
    }

As you can see, its simply supposed to write the datetime to the txt file. the service installs and starts fine, but won't write the datetime. I've had a time trying to get this poc to work. It's supposed to be an abstraction of a commonly used pattern here: do some work at an interval. Do you ladies or gentlemen see where I'm going wrong? Here is the base class:

using System;
using System.ServiceProcess;
using System.Timers;

namespace WF.EMC.AVP.CommonCore.Utilities
{
public abstract class PollingServiceBase : ServiceBase
{
    private readonly Timer _timer;
    private System.ComponentModel.IContainer components = null;
    private string _serviceName;

    protected PollingServiceBase(int pollingMinutes, string serviceName)
    {
        _serviceName = serviceName;
        var pollingInterval = new TimeSpan(0, 0, pollingMinutes, 0);
        _timer = new Timer(pollingInterval.TotalMilliseconds) {AutoReset = true};
        _timer.Elapsed += timer_Elapsed;
    }

    void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        Work();
    }

     protected override void OnStart(string[] args)
    {
        _timer.Start();
    }

     protected override void OnStop()
     {
         _timer.Stop();
     }

        protected abstract void Work();

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }


        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        public void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.ServiceName = _serviceName;
        }

}
}

And here we have the child:

namespace TestService7
{

public partial class Service1 : PollingServiceBase
{
    public Service1(int pollingMinutes = 1, string serviceName = "TestService7")
        : base(pollingMinutes, serviceName)
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
    }

    protected override void OnStop()
    {
    }

    protected override void Work()
    {
        if (!File.Exists(@"C:\Users\Will\Desktop\Developer stuff\junk text files\reusableservicetest.txt"))
            File.Create(@"C:\Users\Will\Desktop\Developer stuff\junk text files\reusableservicetest.txt");
        File.WriteAllLines(@"C:\Users\Will\Desktop\Developer stuff\junk text files\reusableservicetest.txt",
            new[] { DateTime.Now.ToLongTimeString() });
    }
}
}

Upvotes: 0

Views: 56

Answers (1)

Igos
Igos

Reputation: 211

You overridden OnStart with empty method. So your timer will not start.

Upvotes: 3

Related Questions