0

我有一份报告大约需要 2 或 3 分钟才能提取所有数据

所以我试图使用 ASP.net 异步页面来防止超时。但无法让它工作

这就是我正在做的事情:

private delegate List<PublishedPagesDataItem> AsyncReportDataDelegate();

private AsyncReportDataDelegate longRunningMethod;

private List<PublishedPagesDataItem> reportData;

public PublishedPagesReport() // this is the constructor
{
    reportData = new List<PublishedPagesDataItem>();
    longRunningMethod = GetReportData;
}


protected void Page_Load(object sender, EventArgs e)
{
    this.PreRenderComplete +=
        new EventHandler(Page_PreRenderComplete);

    AddOnPreRenderCompleteAsync(
        new BeginEventHandler(BeginAsynchOperation),
        new EndEventHandler(EndAsynchOperation)
    );
}

private List<PublishedPagesDataItem> GetReportData()
{
    // this is a long running method
}

private IAsyncResult BeginAsynchOperation(object sender, EventArgs e, AsyncCallback cb, object extradata)
{
    return longRunningMethod.BeginInvoke(cb, extradata);
}

private void EndAsynchOperation(IAsyncResult ar)
{
    reportData = longRunningMethod.EndInvoke(ar);
}

private void Page_PreRenderComplete(object sender, EventArgs e)
{
    reportGridView.DataSource = reportData;
    reportGridView.DataBind();
}

所以我有一个代表长期运行方法(GetReportData)的委托。

我试图按照这篇文章来称呼它:

http://msdn.microsoft.com/en-us/magazine/cc163725.aspx

长时间运行的方法确实在调试器中完成,但 EndAsynchOperation 和 Page_PreRenderComplete 方法上的断点永远不会被命中

知道我做错了什么吗?

4

1 回答 1

0

下面的代码有效。不知道有什么区别,除了有一个if (!IsPostBack)

反正现在解决了

private delegate List<PublishedPagesDataItem> AsyncReportDataDelegate();

private AsyncReportDataDelegate longRunningMethod;

private List<PublishedPagesDataItem> reportData;

public asynchtest()
{
    reportData = new List<PublishedPagesDataItem>();
    longRunningMethod = GetReportData;
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // Hook PreRenderComplete event for data binding
        this.PreRenderComplete +=
            new EventHandler(Page_PreRenderComplete);

        // Register async methods
        AddOnPreRenderCompleteAsync(
            new BeginEventHandler(BeginAsyncOperation),
            new EndEventHandler(EndAsyncOperation)
        );
    }
}
IAsyncResult BeginAsyncOperation(object sender, EventArgs e,
    AsyncCallback cb, object state)
{
    return longRunningMethod.BeginInvoke(cb, state);
}

void EndAsyncOperation(IAsyncResult ar)
{
    reportData = longRunningMethod.EndInvoke(ar);
}

protected void Page_PreRenderComplete(object sender, EventArgs e)
{
    Output.DataSource = reportData;
    Output.DataBind();
}
于 2010-06-23T11:25:55.583 回答