当将表单从一侧移动到另一侧时,它会在每5秒(使用计时器)检查来自网站的JSON请求时给出一个小的延迟,有什么办法可以防止这个问题吗?
示例图片
以下是代码
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
答案 0 :(得分:2)
使用Asynchronous操作可以解决您的问题
我无法正确测试代码,但如下所示,将sub
更改为Async Sub
并将GetResponse
更改为GetResponseAsync
添加{{1} keword应该这样做。
await