I'm trying to make basic windows service that should write into application event log some text once every minute. So I'm using a timer. But the Tick event doesn't fire.. I've tried all kinds of timer1.start() or timer1.enabled=true ... but nothing works. Everything with service itself is ok- I mean it can be installed, started and onStart() and onStop() it writes event log (also custom, not only the windows default, so the writing itself works..)
So here's the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Threading;

namespace SERVICE2
    public partial class SERVICE2 : ServiceBase
        public SERVICE2()
        protected override void OnStart(string[] args)
            timer1.Enabled = true;
            string src = "Test_Service_2";
            string lg = "Application";
            string evnt = "SERVICE2 started!";
            if (!EventLog.SourceExists(src))
                EventLog.CreateEventSource(src, lg);
            EventLog.WriteEntry(src, evnt);

        protected override void OnStop()
            string src2 = "Test_Service_2";
            string lg2 = "Application";
            string evnt2 = "SERVICE2 stopped!";
            if (!EventLog.SourceExists(src2))
                EventLog.CreateEventSource(src2, lg2);

            EventLog.WriteEntry(src2, evnt2);
            timer1.Enabled = false;

        private void timer1_Tick(object sender, EventArgs e)
            string sSource;
            string sLog;
            string sEvent;

            sSource = "Test_Service_2";
            sLog = "Application";
            sEvent = "Timer TICK";

            if (!EventLog.SourceExists(sSource))
                EventLog.CreateEventSource(sSource, sLog);
            EventLog.WriteEntry(sSource, sEvent);
            EventLog.WriteEntry(sSource, sEvent,
                EventLogEntryType.Warning, 234);

I'd appreciate any help.
Could the problem be in timer properties suck as modifiers: public, or generate members- true?
I'm using Visual Studio 2005

I don't see where you Timer is declared. It's quite possible that it's losing scope and being garbage collected.

This article has been dead for over six months. Start a new discussion instead.