UI hangs when using Async wait

I have this function that freezes the UI:

Public Sub ValidateUrlContentAsync(externalApplyURL As String)

    AsyncManager.OutstandingOperations.Increment()

    Dim result = Threading.Tasks.Task.
        Run( _
            Async Function()
                Return Await ValidateLinks.ValidateUrlContentAsync(externalApplyURL)
            End Function).
        ContinueWith(
            Sub(t)
                Try
                    If t.IsFaulted Then
                        Throw t.Exception
                    End If

                    If t.Result.Error IsNot Nothing Then
                        ErrorHandler.Log.Warn(
                            String.Format("ValidationError: {0}, Exception: {1}", externalApplyURL, t.Result.Error))
                    End If

                    AsyncManager.Parameters("validationResult") = t.Result.ResultValue.ToString()

                Finally
                    AsyncManager.OutstandingOperations.Decrement()
                End Try

            End Sub)

End Sub

 Public Function ValidateUrlContentCompleted(validationResult As String) As ActionResult

    Return Json(validationResult, JsonRequestBehavior.AllowGet)

End Function

      

I thought it task.run

solves this problem because it creates a new thread, separate from the UI thread. What's wrong with this code?

+3


source to share


2 answers


One thing that stands out to me is the use of three separate asynchronous code patterns ( Await

, ContinueWith

and AsyncManager

) instead of using Await

.

Another thing is what you return ActionResult

when pointing to this ASP.NET application and still talk about a "UI thread". There is no UI thread in ASP.NET .

Thus, I interpret "UI hangs" as "does not return until the handler completes," and this is how ASP.NET should work.



So, first remove all unnecessary AsyncManager

, ContinueWith

and Task.Run

codes, which really simplifies the method:

Public Function ValidateUrlContent(externalApplyURL As String) As Task(Of ActionResult)
  Dim result = Await ValidateLinks.ValidateUrlContentAsync(externalApplyURL)
  If result.Error IsNot Nothing Then
    ErrorHandler.Log.Warn(String.Format("ValidationError: {0}, Exception: {1}", externalApplyURL, result.Error))
  End If
  Return Json(result.ResultValue.ToString(), JsonRequestBehavior.AllowGet)
End Function

      

Then solve the "UI freeze" problem. The right place to tackle this problem is in the UI, not on the server side (ASP.NET). The way to prevent the UI from hanging is to invoke the server asynchronously. If you UI is a .NET application, you can use Await

with HttpClient

to call it asynchronously.

+7


source


You must make a call to ValidateUrlContentAsnc. Enable the function and return the task.



0


source







All Articles