Most of the answers are: reference system. web, do not use httpcontext. current. application should use system. web. httpcontext. current. application. Later, I saw an article about system. runtime. remoting. messaging. only detailed introduction of the callcontext class can be found. The original httpcontext. current is based on system. runtime. remoting. messaging. in the callcontext class, neither the subthread nor the asynchronous thread can access the data stored by the main thread in the callcontext. Therefore, httpcontext occurs during asynchronous execution. when current is null, to solve the problem that the sub-thread can obtain the httpcontext of the main thread. current data, you need to set httpcontext before Asynchronization. current is saved as httpcontext and passed in as a parameter. The following shows the implementation method:
CopyCode The Code is as follows: public httpcontext Context
{
Get {return httpcontext. Current ;}
Set {value = context ;}
}
Then create a delegateCopy codeThe Code is as follows: Public Delegate string delegategetresult (httpcontext context );
The following is the encoding of the implementation process.Copy codeThe Code is 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", "");
}
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, winning ";
}
Else
{
Return "I guess you are about to win ";
}
}