1

I have to support an old project which uses PageAsyncTask with webforms vs2010 fw4.

However this simple code does compile + execute( no errors in debug mode / trace mode) but the response never ends.

Looking in debug mode + breakpoints - it reaches all stages of code.

public partial class _Default2 : System.Web.UI.Page
{
 IAsyncResult BeginGetData(object sender, EventArgs e, AsyncCallback callback, object state)
    {
        SqlConnection con = new SqlConnection(@"Data Source=... Asynchronous Processing=True;");
        var sql = @"   SELECT   [NAME] FROM [WebERP].[dbo].[t]";
        {
            SqlCommand _cmd = null;
            try
            {
                _cmd = new SqlCommand(sql, con);
                _cmd.CommandTimeout = 100000;
                con.Open();
                return _cmd.BeginExecuteReader(EndGetData, _cmd);

            }
            catch (Exception ex)
            {
                if (_cmd != null) _cmd.Dispose();
                con.Close();
                throw;
            }
        }
    }

    void EndGetData(IAsyncResult ar)
    {
        (ar.AsyncState as SqlCommand).EndExecuteReader(ar);
        Response.Write(1111); // HttpContext.Current.Response also doesnt help
    }

    void TimeoutData(IAsyncResult ar)
    {
        Response.Write("Could not retrieve data!");
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        PageAsyncTask task = new PageAsyncTask(BeginGetData, EndGetData, TimeoutData, null, true);
        Page.RegisterAsyncTask(task);
        Page.ExecuteRegisteredAsyncTasks();    
    }
}

Question

The response never ends. All I see is :

enter image description here

What am I missing ?

(nb Async="true" is on the page directive , also - code was simplified just to describe the case )

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • I don't think you should pass the same `EndGetData` to both `BeginExecuteReader` and `PageAsyncTask`. – noseratio Jun 13 '14 at 07:54
  • 1
    @Noseratio ouch.... thank you. probably missed that. the correct line should be `return _cmd.BeginExecuteReader(callback(!!!!), state);` where callback declared at `IAsyncResult BeginGetData(object sender, EventArgs e, AsyncCallback callback, object state)` – Royi Namir Jun 13 '14 at 08:07

1 Answers1

1

I think the problem here is that you pass the same BeginGetData and EndGetData callbacks to both BeginExecuteReader and new PageAsyncTask(). They should be different: what you pass to PageAsyncTask is "outer" to what you pass to BeginExecuteReader.

You'd call BeginExecuteReader from the begin callback you'd pass to PageAsyncTask, and you'd be actually required to call the AsyncCallback callback provided to you there by ASP.NET (you'd call it when the async operation would have finished, i.e., from your EndGetData).

It would be so much easier if you could use the PageAsyncTask(Func<Task>) override, but I don't think you can target .NET 4.5 with VS2010.

noseratio
  • 59,932
  • 34
  • 208
  • 486