שתף באמצעות


Can i use an Async function without an Await operator?

Question

Wednesday, January 8, 2020 8:49 PM

I am using a web proxy (Titanium Proxy) as part of a program. It handles 4 different events (before/after request/response). All the handlers are defined as Async, so the signature i am using for before request is:

Async Function Before_Request(Sender As Object, Arguments As SessionEventArgs) As Task Handles Proxy_Server.BeforeResponse

In my case, however, nothing is being awaited for. The event is simply being used to store the current state so Before Response (which indeed uses Await) can get at the correct version of the data. The UI green-lines the function name, telling me that the async method lacks Await operators. If i remove Async from the declaration, it green-lines End Function, telling me that it doesn't return a value on all code paths.

I assume i should leave the Async in there and ignore the green line warning. Does that sound right?

All replies (2)

Wednesday, January 8, 2020 10:18 PM

Hello,

Calling an asynchronous method without await is perfectly fine. Using await and async will keep an app responsive but causes more complexity, especially when exceptions are raised in a sub more so than a function.

If it works now, leave it be, if you are unhappy with "now" then the caller needs to have async keyword and an await for a task.

Sorry at the moment I have no VB examples only C# here.

EDIT here is a simple example where the two buttons call an asynchronous method, one asynchronous and the other synchronous. First button code is responsive, second is not responsive.

Imports System.Net

Public Class Form1
    Private Async Sub AsyncButton_Click(sender As Object, e As EventArgs) _
        Handles AsyncButton.Click

        TextBox1.Text = ""
        Dim response As Boolean = Await InternetAssistant.CheckConnection
        If response Then
            TextBox1.Text = "OnLine"
        Else
            TextBox1.Text = "OffLine"
        End If

    End Sub

    Private Sub SyncButton_Click(sender As Object, e As EventArgs) _
        Handles SyncButton.Click

        TextBox1.Text = ""
        Dim response = Task.Run(Function() InternetAssistant.CheckConnection).Result
        If response Then
            TextBox1.Text = "OnLine"
        Else
            TextBox1.Text = "OffLine"
        End If

    End Sub
End Class
''' <summary>
''' Imagine this is a third party method in a DLL which
''' you can't see.
''' </summary>
Public NotInheritable Class InternetAssistant
    Private Sub New()
    End Sub
    Public Shared Async Function CheckConnection() As Task(Of Boolean)

        ' simulate a longer process
        Await Task.Delay(2000)

        Dim response = Await Task.Run(
            Function()
                Try

                    Using client = New WebClient()
                        Using client.OpenRead("http://google.com/generate_204")
                            Return True
                        End Using
                    End Using
                Catch
                    Return False
                End Try

            End Function)

        Return response

    End Function
End Class

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow


Thursday, January 9, 2020 9:47 AM

Hi,

This is possible.

The async modifier tells the program that this is an asynchronous operation. Awiat will suspend the execution of the code in async, wait for the result after the await expression, skip the async function, and continue executing the following code.

Alternatively, you can use Task instead.

Best Regards,

Julie

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.