4

我试图让用户控件异步工作,但无论我做什么,它都会继续同步工作。作为测试 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>

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

4

2 回答 2

7

看起来我可以回答我自己的问题。用户控件不应该调用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, m_oConnection);
        m_oConnection.Open();

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

    IAsyncResult BeginHandler(object src, EventArgs e, AsyncCallback cb, 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>
于 2008-11-07T21:36:06.300 回答
1

如果我可以在上面的帖子中添加一点,ExecuteRegisteredAsyncTassk除非有令人信服的理由,否则我们不应该显式调用。OnPrerender注册异步任务后,ASP.NET 框架将在页面生命周期事件之后立即执行所有这些任务。

ExecuteRegisteredAsyncTasks使用可能的示例是;有时您可能需要确保在调用另一个异步任务之前完成了几个异步操作。在这种情况下,使用ExecuteRegisteredAsyncTasks.

于 2011-03-08T08:09:55.957 回答