views:

337

answers:

3

I have created a C# service but it fails to starts. I get the following message when I attempt to start it:

The System Usage Monitor service on Local Computer started and then stopped. Some services > stop automatically if they have no work to do, for example, the Performance Logs and Alerts > service.

The following is my OnStart override ...

  /// <summary>
  /// OnStart(): Put startup code here
  ///  - Start threads, get inital data, etc.
  /// </summary>
  protected override void OnStart(string[] args)
     {
     base.OnStart(args);

     broadcaster = new UdpBroadcaster(IP_Address, Port);
     itm = new IdleTimeMonitor(1 * 1 * 3000, 1000);
     aam = new ActiveApplicationMonitor(1000);

     itm.IdleTimeExceeded += new IdleTimeExceededDelegate(itm_IdleTimeExceeded);
     itm.IdleTimeReset += new IdleTimeResetDelegate(itm_IdleTimeReset);
     itm.IdleTimeEvaluated += new IdleTimeEvaluatedDelegate(itm_IdleTimeEvaluated);

     aam.StartedUsingApplication += new StartedUsingApplicationDelegate(aam_StartedUsingApplication);
     aam.EndedUsingApplication += new EndedUsingApplicationDelegate(aam_EndedUsingApplication);
     aam.ApplicationEvaluated += new ApplicationEvaluatedDelegate(aam_ApplicationEvaluated);
     }

Do I need to block at the end of that function or something? Why wont my service start?

+2  A: 

Take a look in the event log, if your service is starting then crashing for some reason there will be an exception event in the Application Log.

Whisk
If it crashed that would have been logged. The message they've received has come from the event log.
Spence
Spot on! One of my threads were failing to start because of a null reference exception :)
Nippysaurus
+2  A: 

Did you kick off a thread?

If you don't have a thread that does something then the application will close so when onstart finishes there is no thread (unless you kick one off in UdpBrodacaster) so the service closes.

EDIT: Just declare a ManualResetEvent that isn't signalled and have the thread call WaitOne() on it. Then in your OnStop() signal the event (.Set()) to have the thread wake up and exit thus closing your service.

Spence
Yeah I have threads ... but they failed to start.
Nippysaurus
A: 

Put your code in the OnStart(string[]) in a try-catch-Block and log the exception to a log file (if one is raised). I think there is a problem on creating the instances.

Jehof