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