Asynchronous httpcontext. Current... )

Source: Internet
Author: User

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 ";
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.