Your program is critical and you must keep it running. You want to use a Timer to periodically check to make sure it is working correctly. Run diagnostics in the Timer event and ensure your site is functioning. Here we look at System.Timers in the C# programming language and discuss ways they can improve your program.
You can use the System.Timers namespace to access the Timer class. Timers are used for monitoring processes such as websites. This tutorial shows the Timer being used in an ASP.NET website.
First, the code that follows is a static class, meaning it cannot have instance members or fields. It includes the System.Timers namespace and shows the Elapsed event function. Just for the example, the code appends the current DateTime to a List every three seconds. Further down in the tutorial, we see the code that reads the List.
~~~ Class that uses Timer (C#) ~~~
using System;
using System.Collections.Generic;
using System.Timers;
public static class TimerExample // In App_Code folder
{
static Timer _timer; // From System.Timers
static List<DateTime> _l; // Stores timer results
public static List<DateTime> DateList // Gets the results
{
get
{
if (_l == null) // Lazily initialize the timer
{
Start(); // Start the timer
}
return _l; // Return the list of dates
}
}
static void Start()
{
_l = new List<DateTime>(); // Allocate the list
_timer = new Timer(3000); // Set up the timer for 3 seconds
//
// Type "_timer.Elapsed += " and press tab twice.
//
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
_timer.Enabled = true; // Enable it
}
static void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
_l.Add(DateTime.Now); // Add date on each timer event
}
}MSDN states that System.Timers "allows you to specify a recurring interval at which the Elapsed event is raised in your application. You can then handle this event to provide regular processing."
Another explanation. Using Timer for periodic checks is a really common requirement and it can work very well. In fact, Microsoft recommends it. Next, MSDN notes that "you could create a service that uses a Timer to periodically check the server and ensure that the system is up and running."
Your ASP.NET application should have an App_Code folder, and you can put a C# .cs file in it that will store a static timer object. For reference, I have materials on global variables in ASP.NET, which apply to this subject.
Here we look at the author's notes on properties, methods and events from the Timer class in the .NET Framework. As shown above, you need to add the System.Timers namespace at the top of your file for easy access to Timer.
Timer.AutoReset MSDN: this indicates "whether the Timer should raise the Elapsed event each time the specified interval elapses or only after the first time it elapses." That means if you want a recurring timer, leave this as true. Timer.Enabled "Whether the Timer should raise the Elapsed event." You must set this to true if you want your timer to do anything. Timer.Interval This indicates "the time, in milliseconds, between raisings of the Elapsed event. The default is 100 milliseconds." You will almost certainly want to make this interval longer than the default. For example, for 30 seconds, use 30000 as the Interval. Dispose Disposed Timers allocate system resources, so if you are creating a lot of them, make sure to Dispose them. This gets complicated fast. I suggest just using a single static timer. Timer.Start This does the same thing as setting Enabled to true. I am not certain why we need this duplicate method. Timer.Stop This does the same thing as setting Enabled to false. See the Timer.Start method shown previously. Timer.Elapsed Event This is the event that is invoked each time the Interval of the Timer has passed. You must specify this function in your code. To add the event, you can press tab twice after typing "_timer.Elapsed +=".
Your .aspx file should have a code-behind file, and this next tutorial example shows how you can use the first code in this article. If your project uses web forms, you should assign the List values to asp:Literal, asp:Label, or other objects.
~~~ ASPX file that uses Timer (C#) ~~~
using System;
using System.Web;
using System.Web.UI;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpResponse r = Response; // Get reference to Response
foreach (DateTime d in TimerExample.DateList) // Get the timer results
{
r.Write(d); // Write the DateTime
r.Write("<br/>"); // Write a line break tag
}
}
}Description. It starts the Timer from the DateList property accessor, and then writes all the contents of the List to the page output. This shows us that the timer code is working. With this Default.aspx page, you can use the Reload button in your web browser, and you will see the timer is adding to the List every 3 seconds.
AJAX timer notes. Note that this tutorial does not show an AJAX timer, and for live page updates, you will need a different client-side mechanism. AJAX timers would not work for critical server-side checks.
Often developers can hit an "Object reference not set to an instance of an object" error when using HttpContext.Current in the Timer. This is because the Timer is invoked on a separate thread. The author does not fully understand why this is, but you can work around the problem by storing important variables in static, global fields or properties.
Here we note how you can use a Timer instance to monitor your ASP.NET site. You can check the filesystem for changes to the App_Data folder, and when new files are detected, they are parsed and checked for errors. This way, the website should almost always use the most recent valid file. Again, for mission-critical sites, you should have logic that tries to detect all errors and then handle them. This can mean visitors to your site don't encounter the errors and you detect them on the Timer.
Here we looked at the Timer class from the System.Timers namespace in the C# programming language targeting the .NET Framework. This interval-based validation approach is recommended by Microsoft for mission-critical applications. If you are maintaining or developing an important site, using Timers to try to handle all errors and report them is an excellent technique.