The answer is also most are: Quote system.web, do not use HttpContext.Current.Application should use System.Web.HttpContext.Current.Application, Later on the Internet to see an article on the System.Runtime.Remoting.Messaging.CallContext this class of detailed introduction to know, Originally HttpContext.Current is based on the System.Runtime.Remoting.Messaging.CallContext class, neither child threads nor asynchronous threads can access the data saved by the main thread in CallContext. Therefore, the process of executing asynchronously will result in a httpcontext.current null situation, in order to solve the thread can get httpcontext.current data of the main thread, Need to be in the asynchronous front to save the httpcontext.current in a HttpContext way, and then pass the parameters of the form passed in, the following see the implementation of the method:
Copy Code code as follows:
Public HttpContext context
{
get {return httpcontext.current;}
set {value = context;}
}
Then create a delegate
Copy Code code as follows:
Public delegate String Delegategetresult (HttpContext context);
The following is the coding of the implementation process
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
context = HttpContext.Current;
Delegategetresult dgt = Testasync;
IAsyncResult IAR = dgt. BeginInvoke (context, NULL, NULL);
string result = Dgt. EndInvoke (IAR);
Response.Write (Result);
}
public static string Testasync (HttpContext context)
{
if (context. application["Booltts"] = = null)
{
Hashtable ht = (Hashtable) context. application["TTS"];
if (HT = = NULL)
{
HT = new Hashtable ();
}
if (ht["A"] = = null)
{
Ht. ADD ("A", "a");
}
if (ht["B"] = = null)
{
Ht. ADD ("B", "B");
}
Context. application["TTS"] = HT;
}
Hashtable hts = new Hashtable ();
HTS = (Hashtable) context. application["TTS"];
if (hts["A"]!= null)
{
Return "Congratulations, the jackpot";
}
Else
{
Return "I guess you're going to win the lottery";
}
}