zoukankan      html  css  js  c++  java
  • asp.net 2.0中异步调用数据

                今天在老外的网上发现在一篇asp.net 2.0 中异步调用的文章。。。。。
        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>
            
    &nbsp;

        
    </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;
    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();

        }

    }


     
        
  • 相关阅读:
    DevExpress WinForms v21.1 重磅升级桑基图控件
    UI组件库Kendo UI for Angular R3 2021新版亮点 支持Bootstrap 5
    Java随笔
    oracle 排序性能 监控,Oracle 常用性能监控SQL语句
    Oracle数据库管理常用的监控脚本极大的简化运维工作
    oracle误删除数据的恢复方法
    Oracle数据库之同义词
    MVCC详解
    goldengate classic extract在什么情况需要重建以及如何重建
    Oracle OGG Kafka
  • 原文地址:https://www.cnblogs.com/gwazy/p/208079.html
Copyright © 2011-2022 走看看