|
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.