0

I'm setting up a asp.net web server.

In page_load, i use async/await.

I want to know if the page thread can do something else while the operation is doing async / await.

For example, Can a page thread handle another page?

 protected void Page_Load(object sender, EventArgs e)
 {  
      //...
      var taskResult = LoadData(sqlConn, sqlTran, 30)

      if(taskResult.Result == true)
      //...
 }

 public static async Task<Boolean> LoadData(SqlConnection sqlConn, SqlTransaction sqlTran, Int64 ID)
 {
     //...
     using (var cmd = new SqlCommand("Proc_LoadData", sqlConn, sqlTran))
     {
         //..
         using (var sqlData = await cmd.ExecuteReaderAsync().ConfigureAwait(false))
         {
             //..
         }
     }
     //..
 }
lowpoly
  • 3
  • 2
  • No, page tread can not handle another page, and you probably don't want it to. The worker process is designed to handle a single request and return a response. What you *can* do is perform several long running tasks in parallel, instead of in sequence. – Jon P Sep 06 '19 at 03:12

1 Answers1

0

In your code there, no, because your Page_Load method is not async, so it's sitting around waiting for the LoadData to finish before it continues. It looks like you are using web forms so I'm not sure that this will work (it's been many years since I've used web forms), but you need to make that method async and then await the other method.

protected async void Page_Load(object sender, EventArgs e)
{  
  //...
  bool taskResult = LoadData(sqlConn, sqlTran, 30)

  if(taskResult == true)
  //...
}

However, if there's an exception thrown in there, you'll never know, because when you have an async void method, there's nothing to handle the exception. Take a look at the answer to Async Void, ASP.Net, and Count of Outstanding Operations by Stephen Cleary, who is THE guru of async/await.

howcheng
  • 2,211
  • 2
  • 17
  • 24
  • This very likely won't work, as nothing in Webforms that calls this method will take care of the async state machine –  Sep 05 '19 at 18:47