Click here to Skip to main content
15,891,821 members
Articles / Web Development / XHTML

How to execute long running jobs from a web client (using ASP.NET 1.1 and 2.0)

Rate me:
Please Sign up or sign in to vote.
4.32/5 (7 votes)
14 Dec 2007CPOL2 min read 35.2K   247   30  
An useful way to call time consuming back-end processes/Stored Procedure calls from the client.
namespace AsyncTransaction
{
	using System;
	using System.Data;
	using System.Configuration;
	using System.Web;
	using System.Threading;

	/// <summary>
	/// Summary description for AsyncRequestResult.
	/// </summary>
	public class AsyncRequestResult : IAsyncResult
	{
		private HttpContext context;
		private AsyncCallback callback;

		private ManualResetEvent completeEvent = null;
		private object data;
		private object objLock = new object();
		private bool isComplete = false;
		
		/// <summary>
		/// Default constructor
		/// </summary>
		/// <param name="ctx">Current Http Context</param>
		/// <param name="cb">Callback used by ASP.NET</param>
		/// <param name="d">Data set by the calling thread</param>
		public AsyncRequestResult(HttpContext ctx, AsyncCallback cb, object d)
		{
			this.context = ctx;
			this.callback = cb;
			this.data = d;			
		}

		/// <summary>
		/// Gets the current HttpContext associated with the request
		/// </summary>
		public HttpContext Context
		{
			get { return this.context; }
		}

		/// <summary>
		/// Completes the request and tells the ASP.NET pipeline that the 
		/// execution is complete.
		/// </summary>
		public void Complete()
		{
			isComplete = true;

			// Complete any manually registered events
			lock(objLock)
			{
				if(completeEvent != null)
				{
					completeEvent.Set();
				}
			}

			// Call any registered callback handers
			// (ASP.NET pipeline)
			if (callback != null)
			{
				callback(this);
			}
		}

		/// <summary>
		/// Performs basic clean up for the call
		/// </summary>
		public void CleanUp()
		{
			
		}

		/// <summary>
		/// Execute long running stored procedure
		/// </summary>
		/// <param name="strCycle">Cycle number</param>
		/// <returns>message.</returns>
		public string RunJob(string strProcessNo)
		{
			string message = "";

			try
			{	
				// Actual Process Call will go here
				System.Threading.Thread.Sleep(10000);
				message = "PROCESS# "+strProcessNo+" Completed";
				
			}
			catch (Exception)
			{
				message = "PROCESS# "+strProcessNo+" Failed";
			}				
			finally
			{
			}
			
			return message;
		}

		#region IAsyncResult Members

		/// <summary>
		/// Gets the object on which one could perform a lock
		/// </summary>
		public object AsyncState
		{
			get { return this.data; }
		}

		/// <summary>
		/// Always returns false
		/// </summary>
		public bool CompletedSynchronously
		{
			get { return false; }
		}

		/// <summary>
		/// Gets a handle that a monitor could lock on.
		/// </summary>
		public WaitHandle AsyncWaitHandle
		{
			get
			{
				lock(objLock)
				{
					if (completeEvent == null)
						completeEvent = new ManualResetEvent(false);

					return completeEvent;
				}
			}
		}

		/// <summary>
		/// Gets the current status of the request
		/// </summary>
		public bool IsCompleted
		{
			get { return this.isComplete; }
		}

		#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
Rajib is one of the many altruist guy working with Cognizant Technology Solution ... Smile | :)

Comments and Discussions