How to pass the context of the current request as a parameter to multiple threads

Source: Internet
Author: User

The multi-threaded program may still run after the request is executed. Therefore, the HttpContext object cannot be directly used in multi-threaded Code. However, we can use it as a parameter and pass it to the multi-threaded code. To implement this function, you can use the following two methods:
1. Pass the HttpContext object as a member of the class. The following is the complete instance code of this method, which can be directly copied and executed:

ASPX code
<% @ Page Language = "C #" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Script runat = "server">
Public class ThreadWork
{
Public System. Web. HttpContext ctx {set; get ;}
Public ThreadWork (){}
Public void DoTest ()
{
If (System. IO. File. Exists (HttpRuntime. AppDomainAppPath + "Log.txt "))
{
System. IO. File. Delete (HttpRuntime. AppDomainAppPath + "Log.txt ");
}

For (int I = 0; I <10; I ++)
{
// Write the content to the file to record the test results.
Using (System. IO. StreamWriter sw = new System. IO. StreamWriter (HttpRuntime. AppDomainAppPath + "Log.txt", true ))
{
Sw. writeLine ("current time:" + DateTime. now. toString ("yyyy-MM-dd HH: mm: ss") + "parameter:" + ctx. request. queryString ["id"]);
Sw. Flush ();
Sw. Close ();
Sw. Dispose ();
}
System. Threading. Thread. Sleep (5000 );
}
}
}

Protected void Page_Load (object sender, EventArgs e)
{
ThreadWork tw = new ThreadWork ();
Tw. ctx = System. Web. HttpContext. Current;
System. Threading. Thread thread = new System. Threading. Thread (new System. Threading. ThreadStart (tw. DoTest ));
Thread. Start ();
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server"> </form>
</Body>
</Html>
We can use this method for access,
Aspx? Id = mengxianhui "> http: // localhost: 11249/WebSite1/ThreadTest. aspx? Id = mengxianhui
The result is as follows:

Txt code
Current Time: 20:11:07 get the parameter: mengxianhui
Current Time: 20:11:12 get the parameter: mengxianhui
Current Time: 20:11:17 get the parameter: mengxianhui
Current Time: 20:11:22 get the parameter: mengxianhui
Current Time: 20:11:27 get the parameter: mengxianhui
Current Time: 20:11:32 get the parameter: mengxianhui
Current Time: 20:11:37 get the parameter: mengxianhui
Current Time: 20:11:42 get the parameter: mengxianhui
Current Time: 20:11:47 get the parameter: mengxianhui
Current Time: 20:11:52 get the parameter: mengxianhui
2. Use the ParameterizedThreadStart class. The complete source code is as follows:
ASpX code
<% @ Page Language = "C #" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Script runat = "server">
Protected void Page_Load (object sender, EventArgs e)
{
System. Threading. Thread thread = new System. Threading. Thread (new System. Threading. ParameterizedThreadStart (DoTest ));
Thread. IsBackground = true;
Thread. Start (System. Web. HttpContext. Current );
Response. Write ("Page request completed. ");
}

Private static void DoTest (Object context)
{
System. Web. HttpContext ctx = context as System. Web. HttpContext;
If (System. IO. File. Exists (HttpRuntime. AppDomainAppPath + "Log.txt "))
{
System. IO. File. Delete (HttpRuntime. AppDomainAppPath + "Log.txt ");
}

For (int I = 0; I <10; I ++)
{
// Write the content to the file to record the test results.
Using (System. IO. StreamWriter sw = new System. IO. StreamWriter (HttpRuntime. AppDomainAppPath + "Log.txt", true ))
{
Sw. writeLine ("current time:" + DateTime. now. toString ("yyyy-MM-dd HH: mm: ss") + "parameter:" + ctx. request. queryString ["id"]);
Sw. Flush ();
Sw. Close ();
Sw. Dispose ();
}
System. Threading. Thread. Sleep (5000 );
}
}
</Script>

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server"> </form>
</Body>
</Html>
The result is the same as the preceding one.

Related Article

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.