使用Async Sub或Functions更新UI

时间:2017-11-08 07:59:35

标签: vb.net asynchronous

我正在学习如何使用Async和Await。我编写了这段代码来获取一个网站,然后每隔一秒就在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 上写一次。问题是,如果网络速度减慢,页面的提取也会减慢,并且UI会暂时冻结。这是代码:

{{1}}

0 个答案:

没有答案