Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

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).

What if the Hokey-Pokey IS what it's all about?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...