ASP. NET multithreading is easy to use in Web pages

Source: Internet
Author: User

Requirements:
A Web page default.aspx contains two controls gridview1,gridview2, which load bound data by two threads respectively.

binding GridView1:
void Bindcategory ()
{


SqlConnection conn = Returnsqlconn ();
SqlCommand comm = new SqlCommand ("SELECT * from category", Conn);
Conn. Open ();
SqlDataReader SDR = Comm. ExecuteReader ();


Gridview1.datasource = SDR;
Gridview1.databind ();
}

binding GridView2:

void Bindnews ()
{
SqlConnection conn = Returnsqlconn ();
SqlCommand comm = new SqlCommand ("SELECT * from News", conn);
Conn. Open ();
SqlDataReader SDR = Comm. ExecuteReader ();


Gridview2.datasource = SDR;
Gridview2.databind ();


}

load two methods, bind data:

protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Try
{
Thread categorythread = new Thread (new ThreadStart (bindcategory));
Thread newsthread = new Thread (new ThreadStart (bindnews));


Categorythread.start ();
Newsthread.start ();


Categorythread.join ();
Newsthread.join ();
}
catch (Exception ex)
{
Response.Write (ex);
}
}
}


Achieve Results


Attention:
Categorythread.join ();
Newsthread.join ();
These two functions are critical, otherwise the page binding fails; The Join method synchronizes the creation of the 2 threads with the page load, and the join is interpreted on msnd by blocking the calling thread until a thread terminates until the standard COM and SendMessage message pump processing continues.

This approach enables multithreading of individual pages to improve efficiency.

Beijing website Construction

ASP. NET multithreading is easy to use in Web pages

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.