使用异步页面的 Asp.net 长时间运行进程

发布于 2024-09-06 09:18:18 字数 1598 浏览 5 评论 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 方法上的断点永远不会被击中,

知道我做错了什么吗?

I have a report that takes about 2 or 3 minutes to pull all the data

So I am trying to use ASP.net Asynch pages to prevent a timeout. But can't get it to work

Here's what I am doing :

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

So I have a delegate representing the Long running method (GetReportData).

And I am trying to call it as per this article :

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

The long running method does complete in the debugger but breakpoints on the EndAsynchOperation and Page_PreRenderComplete methods never get hit

any idea what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小巷里的女流氓 2024-09-13 09:18:18

下面的代码有效。不知道有什么区别,除了有一个 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();
}

the code below works. not sure what the difference is, except that there is a if (!IsPostBack)

anyway, now solved

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文