Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8013159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:33:32+00:00 2026-06-04T19:33:32+00:00

I have this class which fires a method and forget it.. the only problem

  • 0

I have this class which fires a method and forget it.. the only problem is, if the method am calling has an HttpContext it throws NullReferenceException.

My gues is that I can’t use Httpcontext inside the ThreadPool.QueueUserWorkItem(dynamicInvokeShim, new TargetInfo(d, args)); because I am getting NullReferenceException is there a work around for it?

Method with Httpcontext:

public static DataTable GetDataTable(string name)
{
    return (DataTable)HttpContext.Current.Cache[name];
}

method that fires and forget a method:

using System;
using System.Threading;

namespace XGen.Kuapo.BLL
{
  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());
          }
      }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:33:34+00:00Added an answer on June 4, 2026 at 7:33 pm

    Like usr said: HttpContext.Current is only available on the thread executing a request, and a thread pool thread you started is not that same thread.

    However, if you want to access the ASP.NET cache, you can also use HttpRuntime.Cache, which is a static property which is available from each thread. (HttpContext.Current.Cache simply returns HttpRuntime.Cache so you don’t need to worry about any difference.)

    Note that it is ill advised to pass the HttpContext instance received from HttpContext.Current to another thread: by the time the other thread runs, the request corresponding with the captured HttpContext could already have ended, so you could end up with an HttpContext instance that is broken.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which has implemented INotifyPropertyChanged . This class UserInfo has a
I have a Customer class which has a representative field....this field is initially blank
I have this class which I want to pass around Windows as LPARAM parameter.
I have class A which extends the Activity class. This class is in package
I have this script which basically toggles a bgColor class on and off so
I have this middleware which is the first: class RedirectIt require net/https require uri
I have this code which compiles and works as expected: class Right {}; class
Hi Experts: I have a class which extends JPanel now ShapePanel (Sp) this gets
I have written an NHibernateSessionFactory class which holds a static Nhibernate ISessionFactory. This is
I have class Employee which is something like this. class Emp { int EmpID;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.