Click here to Skip to main content
15,881,644 members
Articles / Programming Languages / C#

A New Task Scheduler Class Library for .NET

Rate me:
Please Sign up or sign in to vote.
4.93/5 (233 votes)
17 Dec 2007CPOL15 min read 3.6M   63.3K   634  
A revision of a Task Scheduler class library by David Hall
using System;
using System.Collections;

namespace TaskScheduler
{
	/// <summary>
	/// Deprecated.  Provided for V1 compatibility only. 
	/// </summary>
	/// <remarks>
	/// <p>Presents the Scheduled Tasks folder as a Task Collection. </p> 
	/// 
	/// <p>A TaskList is indexed by name rather than position.
	/// You can't add, remove, or assign tasks in TaskList.  Accessing
	/// a Task in the list by indexing, or by enumeration, is equivalent to opening a task
	/// by calling the ScheduledTasks Open() method.</p> 	
	/// <p><i>Provided for compatibility with version one of the library.  Use of Scheduler
	/// and TaskList will normally result in COM memory leaks.</i></p>
	/// </remarks>
	public class TaskList : IEnumerable, IDisposable
	{
		/// <summary>
		/// Scheduled Tasks folder supporting this TaskList.
		/// </summary>
		private ScheduledTasks st = null;

		/// <summary>
		/// Name of the target computer whose Scheduled Tasks are to be accessed.
		/// </summary>
		private string nameComputer;

		/// <summary>
		/// Constructors - marked internal so you have to create using Scheduler class.
		/// </summary>
		internal TaskList()
		{
			st = new ScheduledTasks();
		}

		internal TaskList(string computer) {
			st = new ScheduledTasks(computer);
		}

		/// <summary>
		/// Enumerator for <c>TaskList</c>
		/// </summary>
		private class Enumerator : IEnumerator
		{
			private ScheduledTasks outer;
			private string[] nameTask;
			private int curIndex;
			private Task curTask;

			/// <summary>
			/// Internal constructor - Only accessable through <see cref="IEnumerable.GetEnumerator()"/>
			/// </summary>
			/// <param name="st">ScheduledTasks object</param>
			internal Enumerator(ScheduledTasks st)
			{
				outer = st;
				nameTask = st.GetTaskNames();
				Reset();
			}

			/// <summary>
			/// Moves to the next task. See <see cref="IEnumerator.MoveNext()"/> for more information.
			/// </summary>
			/// <returns>true if next task found, false if no more tasks.</returns>
			public bool MoveNext()
			{
				bool ok = ++curIndex < nameTask.Length;
				if (ok) curTask = outer.OpenTask(nameTask[curIndex]);
				return ok;
			}

			/// <summary>
			/// Reset task enumeration. See <see cref="IEnumerator.Reset()"/> for more information.
			/// </summary>
			public void Reset()
			{
				curIndex = -1;
				curTask = null;
			}

			/// <summary>
			/// Retrieves the current task.  See <see cref="IEnumerator.Current"/> for more information.
			/// </summary>
			public object Current
			{
				get
				{
					return curTask;
				}
			}
		}
		
		/// <summary>
		/// Name of target computer
		/// </summary>
		internal string TargetComputer
		{
			get
			{
				return nameComputer;
			}
			set
			{
				st.Dispose();
				st = new ScheduledTasks(value);
				nameComputer = value;
			}
		}

		/// <summary>
		/// Creates a new task on the system with the supplied <paramref name="name" />.
		/// </summary>
		/// <param name="name">Unique display name for the task. If not unique, an ArgumentException will be thrown.</param>
		/// <returns>Instance of new task</returns>
		/// <exception cref="ArgumentException">There is already a task of the same name as the one supplied for the new task.</exception>
		public Task NewTask(string name)
		{
			return st.CreateTask(name);
		}

		/// <summary>
		/// Deletes the task of the given <paramref name="name" />.
		/// </summary>
		/// <param name="name">Name of task to delete</param>
		public void Delete(string name)
		{
			st.DeleteTask(name);
		}

		/// <summary>
		/// Indexer which retrieves task of given <paramref name="name" />.
		/// </summary>
		/// <param name="name">Name of task to retrieve</param>
		public Task this[string name]
		{
			get
			{
				return st.OpenTask(name);
			}
		}

		#region Implementation of IEnumerable
		/// <summary>
		/// Gets a TaskList enumerator
		/// </summary>
		/// <returns>Enumerator for TaskList</returns>
		public System.Collections.IEnumerator GetEnumerator()
		{
			return new Enumerator(st);
		}
			#endregion

		#region Implementation of IDisposable
		/// <summary>
		/// Disposes TaskList
		/// </summary>
		public void Dispose()
		{
			st.Dispose();
		}
			#endregion

	}


}

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
Web Developer
United States United States
I'm a long-time programmer in compilers, operating systems, microprogramming, applications, and web programming. I've got way too many years of experience piled up--old Burroughs mainframes, Unix, Macintosh, and Windows.

Software was finally beginning to get boring, but then along came .Net and brought the fun back!

Comments and Discussions