---
title: "Timer in .Net"  
description: "In this blog, I’m trying to explain the concept of timer in .netTheTimercomponent is a server-based timer, which allows you to specify a recurring int"  
author: "Sumit Kesarwani"  
published: 2013-05-14  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/510/timer-in-dot-net  
category: ".net"  
tags: [".net"]  
reading_time: 2 minutes  

---

# Timer in .Net

In this blog, I’m trying to [explain the concept](https://www.mindstick.com/forum/159605/explain-the-concept-of-unique-key-violation-error) of timer in .net

The Timer [component](https://www.mindstick.com/articles/12262/learn-how-to-make-a-component-in-magento-2) is a server-based timer, which allows you to specify a recurring interval at which the [Elapsed](http://msdn.microsoft.com/en-us/library/system.timers.timer.elapsed(v=vs.71).aspx) event is raised in [your application](https://www.mindstick.com/forum/34702/please-tell-me-what-is-cross-site-scripting-and-how-is-it-harmful-for-your-application). You can then handle this event to provide regular processing. For example, suppose you have a [critical](https://www.mindstick.com/blog/11798/4-critical-factors-to-weigh-in-before-finalizing-a-med-school) server that must be kept running 24 hours a day, 7 days a week. You could create a [service](https://www.mindstick.com/articles/105963/online-thesis-writing-service-for-college-kids-with-disabilities) that uses a Timer to periodically check the server and ensure that the system is up and running. If the system is not responding, the service could attempt to restart the server or notify an administrator.

The Timer component raises the Elapsed event, based on the value of the Interval property. You can handle this event to perform the processing you need. For example, suppose that you have an online sales application that continuously posts sales orders to a database. The service that compiles the instructions for [shipping](https://www.mindstick.com/articles/311006/online-retailers-are-starting-to-handle-their-own-shipping) operates on a batch of orders rather than processing each order individually. You could use a Timer to start the [batch processing](https://www.mindstick.com/forum/158518/what-is-the-difference-between-batch-processing-and-real-time-processing-in-big-data-applications) every 30 minutes.

##### Example

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Timers; namespace TimerConsoleApplication2{    class Program    {        static void Main(string[] args)        {            System.Timers.Timer atimer = new System.Timers.Timer();            atimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);            // Set the Interval to 5 seconds.            atimer.Interval = 5000;            atimer.Enabled = true;            Console.WriteLine("Press \'q\' to quit the sample.");            while (Console.Read() != 'q') ;        }         // Specify what you want to happen when the Elapsed event is raised.        private static void OnTimedEvent(object source, ElapsedEventArgs e)        {            Console.WriteLine("Hello World!");        }    }}
```

##### Output

![Timer in .Net](https://www.mindstick.com/blogs/0db5488c-e0f9-480a-b4d8-671dc1aef9a4/images/e8434f13-47f1-486c-8bdd-e394808c46be.png)

---

Original Source: https://www.mindstick.com/blog/510/timer-in-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
