Created
June 20, 2018 09:27
-
-
Save sin2akshay/0fb21963a53310070a34efd2f35a1808 to your computer and use it in GitHub Desktop.
Report Scheduler Code using Timers in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //FOR DAILY REPORT | |
| void timer_Elapsed(object sender, ElapsedEventArgs e) | |
| { | |
| //Current Time | |
| TimeSpan now = DateTime.Now.TimeOfDay; | |
| //Sets Runtime to the time when the report is to be run | |
| TimeSpan runTime = TimeSpan.Parse(ConfigurationManager.AppSettings[Constants.RunTime]); | |
| //Creating a 2 minute timespan | |
| TimeSpan start = now + new TimeSpan(0, 0, -timerIntervalInSeconds); | |
| TimeSpan end = now + new TimeSpan(0, 0, timerIntervalInSeconds); | |
| //If the run time is within the 2 minute timespan, start application | |
| if (runTime >= start && runTime <= end) | |
| Start(); | |
| timer.Enabled = true; | |
| } | |
| //FOR WEEKLY REPORT | |
| void timer_Elapsed(object sender, ElapsedEventArgs e) | |
| { | |
| timer.Enabled = false; | |
| DayOfWeek dayOfWeek; | |
| //Sets dayOfWeek to the day mentioned in App.Config | |
| Enum.TryParse<DayOfWeek>(ConfigurationManager.AppSettings[Constants.RunDay], out dayOfWeek); | |
| if (dayOfWeek == DateTime.Today.DayOfWeek) | |
| { | |
| //Sets Runtime to the time when the report is to be run | |
| TimeSpan runTime = TimeSpan.Parse(ConfigurationManager.AppSettings[Constants.RunTime]); | |
| //Creating a 2 minute timespan | |
| TimeSpan start = now + new TimeSpan(0, 0, -timerIntervalInSeconds); | |
| TimeSpan end = now + new TimeSpan(0, 0, timerIntervalInSeconds); | |
| //If the run time is within the 2 minute timespan, start application | |
| if (runTime >= start && runTime <= end) | |
| Start(); | |
| timer.Enabled = true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment