Fire And Forget class for ASP.NET

To reply the email I received this week about an old post of mine from 2005 that I cannot find, I did some digging. This is the class to create a thread for a process that takes a long time to run in ASP.NET without much ADO.

public class AsyncHelper
    {
        class TargetInfo
        {
            internal TargetInfo(Delegate d, object[] args)
            {
                Target = d;
                Args = args;
            }

            internal readonly Delegate Target;
            internal readonly object[] Args;
        }

        private static WaitCallback dynamicInvokeShim = new WaitCallback(DynamicInvokeShim);

        public static void FireAndForget(Delegate d, params object[] args)
        {
            ThreadPool.QueueUserWorkItem(dynamicInvokeShim, new TargetInfo(d, args));
        }

        static void DynamicInvokeShim(object o)
        {
            try
            {
                TargetInfo ti = (TargetInfo)o;
                ti.Target.DynamicInvoke(ti.Args);
            }
            catch (Exception ex)
            {
                // Only use Trace as is Thread safe
                System.Diagnostics.Trace.WriteLine(ex.ToString());
            }
        }
    }

Hope this helps

Cheers

Al

4 Comments

Comments have been disabled for this content.