Articles → .NET WINDOWS SERVICE → Adding Timer Control in windows service

Adding Timer Control in windows service


























Software Requirement





Prerequisite Knowledge





Creation of new project




Picture showing the project explorer of windows service project

Click to Enlarge


Add timer control




Picture showing the timer control in design mode

Click to Enlarge









Picture showing a list of components from which timer component is selected

Click to Enlarge






Set timer’s interval








Picture showing the property window to set the time interval of the timer

Click to Enlarge








Enable timer control














protected override void OnStart(string[] args) {
	timer1.Enabled = true;
}



Add elapsed event for timer control








Picture showing property window to select Elapsed event of the timer

Click to Enlarge



private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {

}



Adding code in elapsed event


private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
	DirectoryInfo dir = new DirectoryInfo(@"c:\karan");
	if (dir.GetFiles().Count() < 10) {
		File.Create(string.Format(@"c:\karan\{0}.txt", System.Guid.NewGuid()));
	}
}







Install and start service















Output




Picture showing the output of the windows service

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, August 21, 2012

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250