I have a login page in an ASP.Net web forms application. On successful login it does some time-consuming database operations which is slowing the login process, so I want to run these asynchronously.
I am trying to do this in the recommended way using RegisterAsyncTask, etc. I have set the @Page Async="true" in the aspx file and implemented the following code in the aspx.vb file (cut down to show the essential elements):
Protected Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
If LoginSuccessful() Then
DoLoginStuff()
End If
End Sub
Private Sub DoLoginStuff()
...
RegisterAsyncTask(New PageAsyncTask(Function() DoExpensiveDbCheck(userKey)))
Page.ExecuteRegisteredAsyncTasks()
...
End Sub
Private Async Function DoExpensiveDbCheck(ByVal UserKey As Integer) As Task
...
response = Await Task.Run(Function() DbService.DoCheck(UserKey))
If response.Success AndAlso response.Items.Count() = 1 Then
Session("DbValueForUseLater") = response.Item[0].Whatever
End If
End Function
However the DoExpensiveDbCheck function never runs (a breakpoint at the start of the function is never hit), and the Session("DbValueForUseLater") is never set.
I thought maybe it doesn't run because the login function finishes by doing a redirect to the home page of the application, so I added the Page.ExecuteRegisteredAsyncTasks() just in case, but this makes no difference.
Interestingly if I create a Task for DoExpensiveDbCheck rather than using RegisterAsyncTask, and do a Wait on this Task then the application completely freezes, and again a breakpoint in DoExpensiveDbCheck is never hit.
Does anyone know what I am doing wrong here?