移动时如何防止形式滞后?

时间:2017-12-18 10:30:19

标签: vb.net forms animation lag ui-thread

当将表单从一侧移动到另一侧时,它会在每5秒(使用计时器)检查来自网站的JSON请求时给出一个小的延迟,有什么办法可以防止这个问题吗?

示例图片

enter image description here

以下是代码

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    Try

        Dim jsonName = New IO.StreamReader(DirectCast(DirectCast(Net.WebRequest.Create("https://www.googleapis.com/youtube/v3/channels?part=snippet&id=" & ChannelID & "&fields=items(id%2Csnippet(title))&key=AIzaSyA1n4M-fo2Y5NHUj0RsvXEAis3H6_lIjRg"), Net.HttpWebRequest).GetResponse, Net.HttpWebResponse).GetResponseStream)
        Dim rqn As Newtonsoft.Json.Linq.JToken = Newtonsoft.Json.Linq.JObject.Parse(jsonName.ReadToEnd)
        ChannelName = $"{rqn.SelectToken("items")(0)("snippet")("title")}"

        Label3.Text = ChannelName

        Dim json = New IO.StreamReader(DirectCast(DirectCast(Net.WebRequest.Create("https://www.googleapis.com/youtube/v3/channels?part=statistics&id=" & ChannelID & "&fields=items(statistics(subscriberCount))&key=AIzaSyA1n4M-fo2Y5NHUj0RsvXEAis3H6_lIjRg"), Net.HttpWebRequest).GetResponse, Net.HttpWebResponse).GetResponseStream)
        Dim rq As Newtonsoft.Json.Linq.JToken = Newtonsoft.Json.Linq.JObject.Parse(json.ReadToEnd)
        Subs = $"{Convert.ToDecimal(rq.SelectToken("items")(0)("statistics")("subscriberCount")).ToString("#,###,###")}"

        Label1.Text = Subs

    Catch ex As Exception

        Timer1.Stop()
        MessageBox.Show("Erro ao tentar Conseguir os Dados do Canal!")

    End Try

End Sub

1 个答案:

答案 0 :(得分:2)

使用Asynchronous操作可以解决您的问题

我无法正确测试代码,但如下所示,将sub更改为Async Sub并将GetResponse更改为GetResponseAsync添加{{1} keword应该这样做。

await