加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET 2.0异步用户控件不起作用

发布时间:2020-12-16 06:34:47 所属栏目:asp.Net 来源:网络整理
导读:我试图让用户控件异步工作,但无论我做什么,它都会继续同步工作.作为一个测试Web应用程序,我已经将它剥离到最低限度.这将是用户控件: %@ Control Language="C#" %script runat="server" SqlConnection m_oConnection; SqlCommand m_oCommand; void Page_Load
我试图让用户控件异步工作,但无论我做什么,它都会继续同步工作.作为一个测试Web应用程序,我已经将它剥离到最低限度.这将是用户控件:

<%@ Control Language="C#" %>
<script runat="server">
    SqlConnection m_oConnection;
    SqlCommand    m_oCommand;

    void Page_Load(object sender,EventArgs e)
    {
        Trace.Warn("Page_Load");
        string strDSN = ConfigurationManager.ConnectionStrings["DSN"].ConnectionString + ";async=true";
        string strSQL = "waitfor delay '00:00:10'; select * from MyTable";

        m_oConnection = new SqlConnection(strDSN);
        m_oCommand = new SqlCommand(strSQL,m_oConnection);
        m_oConnection.Open();

        Page.RegisterAsyncTask(new PageAsyncTask(new BeginEventHandler(BeginHandler),new EndEventHandler(EndHandler),new EndEventHandler(TimeoutHandler),null,true));
        Page.ExecuteRegisteredAsyncTasks();
    }

    IAsyncResult BeginHandler(object src,EventArgs e,AsyncCallback cb,object state)
    {
        Trace.Warn("BeginHandler");
        return m_oCommand.BeginExecuteReader(cb,state);
    }

    void EndHandler(IAsyncResult ar)
    {
        Trace.Warn("EndHandler");
        GridView1.DataSource = m_oCommand.EndExecuteReader(ar);
        GridView1.DataBind();
        m_oConnection.Close();
    }

    void TimeoutHandler(IAsyncResult ar)
    {
        Trace.Warn("TimeoutHandler");
    }
</script>
<asp:gridview id="GridView1" runat="server" />

这将是我主持控件三次的页面:

<%@ page language="C#" trace="true" async="true" asynctimeout="60" %>
<%@ register tagprefix="uc" tagname="mycontrol" src="~/MyControl.ascx" %>
<html>
    <body>
        <form id="form1" runat="server">
            <uc:mycontrol id="MyControl1" runat="server" />
            <uc:mycontrol id="MyControl2" runat="server" />
            <uc:mycontrol id="MyControl3" runat="server" />
        </form>
    </body>
</html>

页面显示没有错误,但页面底部的跟踪显示每个控件实例是同步处理的.我究竟做错了什么?我缺少哪个配置设置?

解决方法

看起来我可以回答我自己的问题.用户控件不应该调用Page.ExecuteRegisteredAsyncTasks.通过这样做,控件添加了异步任务,运行它,并等待它完成.

相反,用户控件的每个实例都应该只调用Page.RegisterAsyncTask.在每个控件实例完成此操作后,页面会自动调用同时运行所有三个已注册异步任务的RegistereAsyncTask.

所以这是新的用户控件:

<%@ Control Language="C#" %>
<script runat="server">
    SqlConnection m_oConnection;
    SqlCommand    m_oCommand;

    void Page_Load(object sender,EventArgs e)
    {
        Trace.Warn(ID,"Page_Load - " + Thread.CurrentThread.GetHashCode().ToString());
        string strDSN = ConfigurationManager.ConnectionStrings["DSN"].ConnectionString + ";async=true";
        string strSQL = "waitfor delay '00:00:10'; select * from TEProcessedPerDay where Date > dateadd(day,-90,getutcdate()) order by Date asc";

        m_oConnection = new SqlConnection(strDSN);
        m_oCommand = new SqlCommand(strSQL,true));
    }

    IAsyncResult BeginHandler(object src,object state)
    {
        Trace.Warn(ID,"BeginHandler - " + Thread.CurrentThread.GetHashCode().ToString());
        return m_oCommand.BeginExecuteReader(cb,state);
    }

    void EndHandler(IAsyncResult ar)
    {
        Trace.Warn(ID,"EndHandler - " + Thread.CurrentThread.GetHashCode().ToString());
        GridView1.DataSource = m_oCommand.EndExecuteReader(ar);
        GridView1.DataBind();
        m_oConnection.Close();
    }

    void TimeoutHandler(IAsyncResult ar)
    {
        Trace.Warn(ID,"TimeoutHandler - " + Thread.CurrentThread.GetHashCode().ToString());
    }
</script>
<asp:gridview id="GridView1" runat="server" />

并且创建三个控件实例的未更改页面:

<%@ page language="C#" async="true" trace="true" %>
<%@ register tagprefix="uc" tagname="mycontrol" src="~/MyControl.ascx" %>
<html>
    <body>
        <form id="form1" runat="server">
            <uc:mycontrol id="MyControl1" runat="server" />
            <uc:mycontrol id="MyControl2" runat="server" />
            <uc:mycontrol id="MyControl3" runat="server" />
        </form>
    </body>
</html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读