views:

30

answers:

1

Hi

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?

A: 

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