Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

Hi

 

I have a static class that acts as a "hearbeat" to allow external listeners to tell if the application is running healthily :-

 

      private static HeartbeatListenerCollection _listeners = new HeartbeatListenerCollection();

       /// <summary>
       /// Sends a heartbeat to any heartbeat monitors
       /// to indicate that the application is running
       /// </summary>
       public static void Beat()
       {
           // make each heartbeat listener beat 
           foreach (HeartbeatMonitorBase  _listener in Listeners)
           {
               _listener.Beat();
           }
       }

       /// <summary>
       /// Starts the heartbeat with a default frequency
       /// </summary>
       static Heartbeat()
       {
           // Fire up a thread to invoke hearbeat events 
           // perdiodically...
           TimerCallback timerDelegate =
           new TimerCallback(Heartbeat.Beat);

           AutoResetEvent autoEvent = new AutoResetEvent(false);

           System.Threading.Timer heartbeatTimer =
               new System.Threading.Timer(timerDelegate, autoEvent, 0,1000);
           

       }


       /// <summary>
       /// The listeners attached to the heartbeat
       /// </summary>
       /// <remarks >
       /// These should not be triggered directly but rather the 
       /// Heartbeat.Beat() method should be called
       /// </remarks>
       public  static HeartbeatListenerCollection Listeners
       {
           get { return Heartbeat._listeners; }
       }

   }

 

However the static class is only instantiated the first time it is referred to (effectively the heart only starts beating if we start listening to it). Can anyone think of aclever way to instantiate this class at application startup automatically?

Printer Monitor for .NET? - see Merrion Computing Ltd for details
Posted

If a tree falls in the woods and nobody is around to hear it...

 

Perhaps a silly question, but if there are no listeners, why does it matter whether the heartbeat thread is running?

 

Assuming this is your application, can you not just call Beat or an initialization method at application start?

Never trouble another for what you can do for yourself.
  • *Experts*
Posted

if there are no listeners, why does it matter whether the heartbeat thread is running?

Ah - there is a supposed to be a default listener. Kind of like the way "Trace" works.

 

Assuming this is your application

I'd like to attach it to existing coded applications

Printer Monitor for .NET? - see Merrion Computing Ltd for details

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