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 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.
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With