Windows Service can't start with System.Threading.Timer

joker77

Freshman
Joined
Sep 16, 2005
Messages
38
Location
Dublin
Hi there,

I'm having trouble starting a very simple Windows Service I created with a System.Threading.Timer. The code is a basic windows service project, with the following lines of note:
Code:
using System.Threading;

public partial class TimerClass: ServiceBase
    {

        private Timer _timer;

        public TimerClass()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            TimerCallback callback = new TimerCallback(OnTimedEvent);
            _timer = new Timer(callback, null, 10000, 60000);
        }

        protected override void OnStop()
        {
            // Stop Timer    
        }

        private void OnTimedEvent(Object stateInfo)
        {
            // Do something           
        }
    }
}

When I try to start the service, I get the following error:
"The TimerService service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs."

If I remove the 2 lines in the OnStart method, the service starts fine.

I've looked around on the internet, and from what I was reading the System.Threading.Timers is the one to use in a Windows Service rather than System.Timers.Timer (obviously the Windows forms one can't be used).
 
Back
Top