Asp.Net多线程用法1

时间:2024-01-12 09:10:50

Asp.Net多线程简单用法

一个web页面 default.aspx 里面有两个控件GridView1,GridView2,通过两个线程分别加载绑定数据。

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
Thread thead1 = new Thread(new ThreadStart(BindPeople));//定义线程
Thread thead2 = new Thread(new ThreadStart(BindNews)); thead1.Start(); //启动线程
thead2.Start(); thead1.Join(); //大概就是和UI线程同步的意思
thead2.Join();
}
catch (Exception ex)
{
Response.Write(ex);
}
}
}
/// <summary>
/// 绑定用户
/// </summary>
private void BindPeople()
{
DataTable dt = DBHelper.GetDataTable("select * from people");
GridView1.DataSource = sdr;
GridView1.DataBind();
}
/// <summary>
/// 绑定新闻
/// </summary>
private void BindNews()
{
DataTable dt = DBHelper.GetDataTable("select * from news");
GridView2.DataSource = sdr;
GridView2.DataBind();
}