http://fredrik.nsquared2.com/viewpost.aspx?PostID=282&showfeedback=true
按原来的代码写了一下,又不用异步试了一下,感觉调用异步稍微快一点,不过不是很明显..
可能是调用异步时,webDev.WebServe 占用内存很大的缘故(不调用异步时占用很少...)。。。。
用过的朋友谈谈....
异步调用。。
注意设置 Async="true"
<%@ Page Language="C#" Async="true" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
BeginInvoke Thread: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
EndInvoke Thtead: <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=true>
</asp:GridView>
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
BeginInvoke Thread: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
EndInvoke Thtead: <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=true>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
private SqlCommand _command;
private SqlConnection _con;
protected void Page_Load(object sender, EventArgs e)
{
PageAsyncTask task = new PageAsyncTask(BeginInvoke, EndInvoke, EndTimeOutInvoke, null);
Page.RegisterAsyncTask(task);
}
public IAsyncResult BeginInvoke(object sender, EventArgs e, AsyncCallback cb, object extraData)
{
this._con = new SqlConnection("Asynchronous Processing=true;Data Source=JKDL-PORTAL;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=111");
string sProcName = "SELECT * FROM Customers";
for (int i = 0; i < 200; i++)
{
sProcName += " UNION ALL ";
sProcName += " SELECT * FROM Customers ";
}
this._command = new SqlCommand(sProcName, this._con);
Label1.Text = System.Threading.Thread.CurrentThread.GetHashCode().ToString();
this._con.Open();
return this._command.BeginExecuteReader(cb, extraData, CommandBehavior.CloseConnection);
}
public void EndInvoke(IAsyncResult result)
{
SqlDataReader reader = this._command.EndExecuteReader(result);
if (reader != null && reader.HasRows)
{
GridView1.DataSource = reader;
GridView1.DataBind();
}
reader.Close();
Label2.Text = System.Threading.Thread.CurrentThread.GetHashCode().ToString();
}
public void EndTimeOutInvoke(IAsyncResult result)
{
if (this._con != null && this._con.State != ConnectionState.Closed)
this._con.Close();
Response.Write("TimeOut");
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
private SqlCommand _command;
private SqlConnection _con;
protected void Page_Load(object sender, EventArgs e)
{
PageAsyncTask task = new PageAsyncTask(BeginInvoke, EndInvoke, EndTimeOutInvoke, null);
Page.RegisterAsyncTask(task);
}
public IAsyncResult BeginInvoke(object sender, EventArgs e, AsyncCallback cb, object extraData)
{
this._con = new SqlConnection("Asynchronous Processing=true;Data Source=JKDL-PORTAL;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=111");
string sProcName = "SELECT * FROM Customers";
for (int i = 0; i < 200; i++)
{
sProcName += " UNION ALL ";
sProcName += " SELECT * FROM Customers ";
}
this._command = new SqlCommand(sProcName, this._con);
Label1.Text = System.Threading.Thread.CurrentThread.GetHashCode().ToString();
this._con.Open();
return this._command.BeginExecuteReader(cb, extraData, CommandBehavior.CloseConnection);
}
public void EndInvoke(IAsyncResult result)
{
SqlDataReader reader = this._command.EndExecuteReader(result);
if (reader != null && reader.HasRows)
{
GridView1.DataSource = reader;
GridView1.DataBind();
}
reader.Close();
Label2.Text = System.Threading.Thread.CurrentThread.GetHashCode().ToString();
}
public void EndTimeOutInvoke(IAsyncResult result)
{
if (this._con != null && this._con.State != ConnectionState.Closed)
this._con.Close();
Response.Write("TimeOut");
}
}
非异步时
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default6 : System.Web.UI.Page
{
private SqlCommand _command;
private SqlConnection _con;
protected void Page_Load(object sender, EventArgs e)
{
this._con = new SqlConnection("Data Source=JKDL-PORTAL;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=111");
string sProcName = "SELECT * FROM Customers";
for (int i = 0; i < 200; i++)
{
sProcName += " UNION ALL ";
sProcName += " SELECT * FROM Customers ";
}
this._con.Open();
this._command = new SqlCommand(sProcName, this._con);
this.GridView1.DataSource = this._command.ExecuteReader();
this.GridView1.DataBind();
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default6 : System.Web.UI.Page
{
private SqlCommand _command;
private SqlConnection _con;
protected void Page_Load(object sender, EventArgs e)
{
this._con = new SqlConnection("Data Source=JKDL-PORTAL;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=111");
string sProcName = "SELECT * FROM Customers";
for (int i = 0; i < 200; i++)
{
sProcName += " UNION ALL ";
sProcName += " SELECT * FROM Customers ";
}
this._con.Open();
this._command = new SqlCommand(sProcName, this._con);
this.GridView1.DataSource = this._command.ExecuteReader();
this.GridView1.DataBind();
}
}