Click here to Skip to main content
15,893,381 members
Articles / Programming Languages / C#

.NET Scheduled Timer

Rate me:
Please Sign up or sign in to vote.
4.89/5 (139 votes)
16 Sep 2005CPOL15 min read 1.1M   38.4K   361  
A timer that easily supports absolute schedules like run at 4:00 AM every day or at 5:00 PM on Fridays..
using System;
using Schedule;

namespace TimerTest
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		static void Main(string[] args)
		{
			try
			{
				RangeTest();
				IScheduledItem item = new SimpleInterval(DateTime.Now, new TimeSpan(0, 0, 3));
//				item = new BlockWrapper(item, "ByMinute", "15", "45");
				_Timer.AddJob(item, _f);
				_Timer.Error += new ExceptionEventHandler(_Timer_Error);
				_Timer.Start();
			} 
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				Console.WriteLine(e.StackTrace);
			}
			finally
			{
				try { while(Console.ReadLine().ToLower() != "q"); } catch {};
			}
		}
		static ScheduleTimer _Timer = new ScheduleTimer();

		private delegate void f(DateTime time);
		static f _f = new f(WriteEvent);

		public static void WriteEvent(DateTime time)
		{
			DateTime actual = DateTime.Now;
			Console.WriteLine("{0} - {1} = {2}", actual, time, (actual-time).TotalMilliseconds);
		}

		private static void _Timer_Error(object sender, ExceptionEventArgs Args)
		{
			Console.WriteLine(Args.EventTime);
			Console.WriteLine(Args.Error.Message);
			Console.WriteLine(Args.Error.StackTrace);
		}

		private static void RangeTest()
		{
			IScheduledItem item = new ScheduledTime("Weekly", "1,0:0:0");
			TestItem(item, DateTime.Parse("1/1/04"), true, DateTime.Parse("1/5/04"));
			TestItem(item, DateTime.Parse("1/2/04"), true, DateTime.Parse("1/5/04"));
			TestItem(item, DateTime.Parse("12/30/03"), true, DateTime.Parse("1/5/04"));
			TestItem(item, DateTime.Parse("1/5/04"), true, DateTime.Parse("1/5/04"));
			TestItem(item, DateTime.Parse("1/5/04"), false, DateTime.Parse("1/12/04"));
			TestItem(item, DateTime.Parse("1/6/04"), false, DateTime.Parse("1/12/04"));
			TestItem(new ScheduledTime("Weekly", "0,0:0:0"), DateTime.Parse("1/1/04"), true, DateTime.Parse("1/4/04"));
			TestItem(new ScheduledTime("Weekly", "1,0:0:0"), DateTime.Parse("1/1/04"), true, DateTime.Parse("1/5/04"));
			TestItem(new ScheduledTime("Weekly", "2,0:0:0"), DateTime.Parse("1/1/04"), true, DateTime.Parse("1/6/04"));
			TestItem(new ScheduledTime("Weekly", "3,0:0:0"), DateTime.Parse("1/1/04"), true, DateTime.Parse("1/7/04"));
			TestItem(new ScheduledTime("Weekly", "4,0:0:0"), DateTime.Parse("1/1/04"), true, DateTime.Parse("1/1/04"));
			TestItem(new ScheduledTime("Weekly", "5,0:0:0"), DateTime.Parse("1/1/04"), true, DateTime.Parse("1/2/04"));
			TestItem(new ScheduledTime("Weekly", "6,0:0:0"), DateTime.Parse("1/1/04"), true, DateTime.Parse("1/3/04"));
			TestItem(new ScheduledTime("Weekly", "4,0:0:0"), DateTime.Parse("1/1/04"), false, DateTime.Parse("1/8/04"));
			TestItem(new ScheduledTime("Weekly", "0,6:34:23"), DateTime.Parse("1/1/04"), true, DateTime.Parse("1/4/04 6:34:23"));
			TestItem(new ScheduledTime("Weekly", "0,6:34:23"), DateTime.Parse("1/4/04"), true, DateTime.Parse("1/4/04 6:34:23"));
			TestItem(new ScheduledTime("Weekly", "0,6:34:23"), DateTime.Parse("1/4/04 3:00:00"), true, DateTime.Parse("1/4/04 6:34:23"));
			TestItem(new ScheduledTime("Weekly", "0,6:34:23"), DateTime.Parse("1/4/04 6:00:00"), true, DateTime.Parse("1/4/04 6:34:23"));
			TestItem(new ScheduledTime("Weekly", "0,6:34:23"), DateTime.Parse("1/4/04 6:33:00"), true, DateTime.Parse("1/4/04 6:34:23"));
			TestItem(new ScheduledTime("Weekly", "0,6:34:23"), DateTime.Parse("1/4/04 6:34:00"), true, DateTime.Parse("1/4/04 6:34:23"));
			TestItem(new ScheduledTime("Weekly", "0,6:34:23"), DateTime.Parse("1/4/04 6:34:23"), true, DateTime.Parse("1/4/04 6:34:23"));
			TestItem(new ScheduledTime("Weekly", "0,6:34:23"), DateTime.Parse("1/4/04 6:34:23"), false, DateTime.Parse("1/11/04 6:34:23"));
			TestItem(new ScheduledTime("Weekly", "0,6:34:23"), DateTime.Parse("1/4/04 6:34:24"), true, DateTime.Parse("1/11/04 6:34:23"));
		}

		private static void TestItem(IScheduledItem item, DateTime input, bool AllowExact, DateTime ExpectedOutput)
		{
			DateTime Result = item.NextRunTime(input, AllowExact);
			if (Result == ExpectedOutput)
				Console.WriteLine("Success");
			else
				Console.WriteLine(string.Format("Failure: Received: {0} Expected: {1}", Result, ExpectedOutput));
		}
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Standard Beagle Studios
United States United States
I co-founded Standard Beagle Studio, a software development consulting service in Austin Texas with my wife Cindy Brummer. We focus mostly on web projects, but have built some react native mobile apps, and even a windows screen saver or two.

I started my career back when ASP pages were state of the art, and IE3 was considered a web browser. I've worked with Microsoft technologies for most of that time, and have recently branched out into node, wordpress, and react native applications.

I'm a web developer, math and physics enthusiast, father of 2, and all around great guy. I live in Austin TX and love using technology to change people's lives for the better. When I manage scrape together some spare time, I build generative art at curvature of the mind.

Comments and Discussions