Home
Manage Your Code
Snippet: Create a Timer (C#)
Title: Create a Timer Language: C#
Description: Create a timer, start it and implement the method to call after configured amount of time has elapsed Views: 531
Author: Artem Kazmerchuk Date Added: 4/10/2007
Copy Code  
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Configuration;
5using System.Timers;
6
7public class TimerExample
8{
9        private static System.Timers.Timer pollingTimer = null;  
10	private static void Initialize()
11        {
12            pollingTimer = new System.Timers.Timer();
13            int pollingInterval = 0;
14            int.TryParse(ConfigurationManager.AppSettings["PollingInterval"], out pollingInterval);
15            pollingTimer.Interval = pollingInterval == 0? 20000 : pollingInterval;
16	    pollingTimer.Elapsed +=new ElapsedEventHandler(OnElapsed);
17        }
18
19        public void OnElapsed(object source, ElapsedEventArgs args)
20        {
21	    //your code here
22	}
23}
24