I want to wait for a line to be read, but only for so long before timing out.  This is what I came up with.  Is there a better way to do it?Dim reader As New System.IO.StreamReader(pipe)Dim nextCommand = Await New Func(Of Task(Of String))(    Function()        Dim t = reader.ReadLineAsync()        If (Not t.Wait(2000)) Then Throw New MyTimeoutExeption()        Return t    End Function).Invoke()
Wait for System.IO.StreamReader.ReadLineAsync
optimization;vb.net;asynchronous;async await
For operations that don't support cancellation themselves, You can combine Task.WhenAny() with Task.Delay():Async Function TryAwait(Of T)(target As Task(Of T), delay as Integer) As Task(Of T)    Dim completed = Await Task.WhenAny(target, Task.Delay(delay))    If completed Is target Then        Return Await target    End If    Throw New TimeoutException()End FunctionUsage:Dim nextCommand = Await TryAwait(reader.ReadLineAsync(), 2000)