如何防止移动时形式滞后?
How to prevent lag in form while moving?
在左右移动表单时,每 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
使用Asynchronous操作可能会解决您的问题
我无法正确测试代码,但如下所示,将 sub
更改为 Async Sub
并将 GetResponse
更改为 GetResponseAsync
添加 await
keword 应该这样做。
Private Async Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Dim jsonName = New IO.StreamReader(DirectCast(Await 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).GetResponseAsync, 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(Await 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).GetResponseAsync, 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
在左右移动表单时,每 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
使用Asynchronous操作可能会解决您的问题
我无法正确测试代码,但如下所示,将 sub
更改为 Async Sub
并将 GetResponse
更改为 GetResponseAsync
添加 await
keword 应该这样做。
Private Async Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Dim jsonName = New IO.StreamReader(DirectCast(Await 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).GetResponseAsync, 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(Await 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).GetResponseAsync, 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