For ASP.Net applications. Starts a thread to perform a task at BelowNormal priority on a regular interval. Meant for long-running ASP.Net apps that never recycle instead of a Windows Service or cron job. If the task runs longer than the timer interval, the existing thread is left be and nothing else happens until the next time the interval fires where the worker thread is not running. Cannot handle multiple tasks; if you have multiple tasks to manage, build on top of this class by adding task registration to a single task that this class runs. The task is defined as an Action<IntervalTaskContext>, where the argument it takes is a context object that has a single property: Stopping. If the ASP.Net app needs to shut down for some reason, and the task is currently running, this flag will flip to true, and the app will be forcibly torn down by the environment in 30 seconds. This means the task should check Stopping regularly to see if it should cut its work short. Note that the 30 seconds is for the entire app to tie up what it's doing, not just this task - so be conservative. For more on the ASP.Net App teardown process: http://msdn.microsoft.com/en-us/library/system.web.hosting.iregisteredobject.stop.aspx http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx
Inheritance: System.Web.Hosting.IRegisteredObject, IDisposable
Beispiel #1
0
        /// <summary>
        /// Creates a new IntervalTask (and doesn't run it - call SetTimerInterval() to start it).
        /// taskAction format: context => { /* do work */ }
        /// If a task has already been created, throws a FieldAccessException.
        /// </summary>
        /// <param name="taskAction">An Action to be run on an interval</param>
        /// <returns>The created IntervalTask. Call SetTimerInterval() on the returned object to start it.</returns>
        public static IntervalTask CreateTask(Action taskAction)
        {
            if (Current != null)
            {
                throw new FieldAccessException("CreateTask requested, but a task already exists.");
            }

            Current = new IntervalTask(taskAction);
            return(Current);
        }
		/// <summary>
		/// Creates a new IntervalTask (and doesn't run it - call SetTimerInterval() to start it).
		/// taskAction format: context => { /* do work */ }
		/// If a task has already been created, throws a FieldAccessException.
		/// </summary>
		/// <param name="taskAction">An Action to be run on an interval</param>
		/// <returns>The created IntervalTask. Call SetTimerInterval() on the returned object to start it.</returns>
		public static IntervalTask CreateTask(Action taskAction)
		{
			if (Current != null)
				throw new FieldAccessException("CreateTask requested, but a task already exists.");

			Current = new IntervalTask(taskAction);
			return Current;
		}