Asp.net使用asynch页面长时间运行进程

时间:2010-06-23 09:49:22

标签: asp.net asynchronous delegates multithreading long-running-processes

我的报告需要大约2到3分钟来提取所有数据

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

这就是我在做的事情:

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

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

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

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

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

知道我做错了什么?

1 个答案:

答案 0 :(得分: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();
}