Select in Visual Basic .NET
Our program demonstrates the use of Select
in Visual Basic .NET, which allows us to wait on multiple asynchronous operations. This is similar to Go’s select statement, but implemented using the Task
class and async/await pattern in .NET.
In this example, we’re using Task.Run
to simulate concurrent operations that complete after a specific delay. The DelayedMessage
function represents an asynchronous operation that returns a message after a specified delay.
We use Task.WhenAny
to wait for either of the tasks to complete, which is similar to the select
statement in the original example. When a task completes, we print its result and mark it as completed to prevent it from being selected again.
To run the program:
Note that the total execution time is only about 2 seconds since both the 1 and 2 second delays execute concurrently.
This example demonstrates how to work with multiple asynchronous operations in Visual Basic .NET, providing functionality similar to Go’s select statement.