Hi,
Check this out...
/Oscar
//Buisness layer...gets called from an ASP.NET web page...
public bool InitAsyncOperation(string someData)
{
try
{
//Access layer
Access access = Access.GetInstance(CurrentUser);
if(access.IsRunning)
{
return false;
}
else
{
//First we do some sync operation...
Wrapper wrapper = access.Validate(someData);
//Then we kick of the async...
MyDelegate d = new MyDelegate(<Another method...that performs the
async operation...>);
AsyncHelper.Execute(d, wrapper);
}
}
catch(Exception ex)
{
if(ExceptionPolicy.HandleException(ex, CONTROLLER_EXCEPTION_POLICY))
throw;
}
return true;
}
///Class AsyncHelper
using System;
using System.Reflection;
using System.Threading;
namespace <SomeCompany>.<SomeApp>.Util
{
/// <summary>
/// Starting with the 1.1 release of the .NET Framework, the SDK docs
/// now carry a caution that mandates calling EndInvoke on delegates
/// you've called BeginInvoke on in order to avoid potential leaks.
/// This means you cannot simply "fire-and-forget" a call to
BeginInvoke
/// when spawning a new worker thread from the thread pool without the
risk
/// of creating a memory leak.
///
/// The usage model is that instead of calling BeginInvoke against a
delegate,
/// you would instead call AsyncHelper.Execute, passing that delegate and
it's parameters as input.
/// See:
http://staff.develop.com/woodring for further information.
/// </summary>
/// <example>
/// delegate void CalcAndDisplaySumDelegate( int a, int b );
/// CalcAndDisplaySumDelegate d = new
CalcAndDisplaySumDelegate(someCalc.Add);
/// AsyncHelper.Execute(d, 2, 3);
/// </example>
public class AsyncHelper
{
private static WaitCallback callback = new
WaitCallback(DynamicInvokeShim);
/// <summary>
/// Takes a delegate and a list of arguments. Performs asynchronous
invocation of the
/// target(the Class.Method the delegates points to..).
/// </summary>
/// <param name="d"></param>
/// <param name="args"></param>
/// <Author>Oscar Thornell</Author>
public static void Execute(Delegate d, params object[] args)
{
ThreadPool.QueueUserWorkItem(callback, new TargetInfo(d, args));
}
private static void DynamicInvokeShim(object obj)
{
TargetInfo targetInfo = (TargetInfo)obj;
targetInfo.Target.DynamicInvoke(targetInfo.Args);
}
class TargetInfo
{
internal readonly Delegate Target;
internal readonly object[] Args;
internal TargetInfo(Delegate d, object[] args)
{
Target = d;
Args = args;
}
}
}
}
JV said:
My ASP.NET application needs to accept data from a post and return
quickly. I've added a worker thread to wake up and save data
asynchronously after the data is received. But, it appears to me that
IIS kills the process and thus the worker thread right after my main
ASP.NET thread returns (anyone know for certain?).
Do you know of a design pattern to accomplish this?