Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should Task.Run call an async method in VB.NET?

Given an asynchronous method that does both CPU and IO work such as:

Public Async Function RunAsync() As Task
    DoWork()
    Await networkStream.WriteAsync(buffer, 0, buffer.Length).ConfigureAwait(False)
End Function

Which of the following options is the best way to call that asynchronous method from Task.Run in Visual Basic and why?

Which is the VB equivalent for C# Task.Run(() => RunAsync())?

Await Task.Run(Function() RunAsync())
' or
Await Task.Run(Sub() RunAsync())

Are the Async/Await keywords within Task.Run necessary or redundant? This comment claims they're redundant, but this answer suggests it might be necessary in certain cases:

Await Task.Run(Async Function()
                   Await RunAsync()
               End Function)

Is ConfigureAwait useful within Task.Run?

Await Task.Run(Function() RunAsync().ConfigureAwait(False))

Await Task.Run(Async Function()
                   Await RunAsync().ConfigureAwait(False)
               End Function)

Which of the above 5 Task.Run options is best practice?

Note: There's a similar question How to call Async Method within Task.Run? but it's for C#, the selected answer has negative votes, and doesn't address ConfigureAwait.

like image 594
Mike Henry Avatar asked Feb 02 '17 18:02

Mike Henry


People also ask

How do I run code asynchronously in VB NET?

This VB.NET page uses the Async and Wait keywords to run code asynchronously. It uses Task, Start and Wait. Async, Await. In asynchronous programming, many control flows can exist at once. With the Async and Await keywords in VB.NET we have a standard, clear way to program this way.

What is the difference between in main and async in VB?

In Main we create a new Task—we must use AddressOf in VB.NET to reference a function for the Task. Start, wait In Main we invoke Start and Wait on the task. The program does not exit until ProcessDataAsync finishes. Async This keyword is used at the function level.

How do I return a task from an async method?

In.NET Framework programming, an async method typically returns a Task or a Task (Of TResult). Inside an async method, an Await operator is applied to a task that's returned from a call to another async method. You specify Task (Of TResult) as the return type if the method contains a Return statement that specifies an operand of type TResult.

What is await and async in VB NET?

Async, Await. In asynchronous programming, many control flows can exist at once. With the Async and Await keywords in VB.NET we have a standard, clear way to program this way. Threading is supported. But with Async and Await, we do not need multiple threads.


1 Answers

Which is the VB equivalent for C# Task.Run(() => RunAsync())?

My VB is horribly rusty, but it should not be the Sub one, so I'd go with:

Task.Run(Function() RunAsync())

Are the Async/Await keywords within Task.Run necessary or redundant?

I have a blog post on the subject. In this case, they're redundant because the delegate is trivial.

Is ConfigureAwait useful within Task.Run?

Only if you do an Await.

like image 162
Stephen Cleary Avatar answered Sep 19 '22 14:09

Stephen Cleary