I'm learning on how to use Async and Await. I wrote this code to fetch a website and write it on a RichTextBox
in every one second. The problem is, if there's a network slowdown, the fetching of the page slows down too and the UI freezes briefly. This is the code:
Imports System.Net
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
Private Async Function FetchData(uri As String) As Task(Of String)
Using wb As New WebClient
Dim stream As New StreamReader(wb.OpenRead(uri))
Return Await stream.ReadToEndAsync
End Using
End Function
Private Async Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
RichTextBox1.Text = Await FetchData("http://someurl")
End Sub
End Class