Vb.net在计时器中添加http请求 [英] Vb.net add http request in a timer
问题描述
我在vb winform应用程序中工作,需要向API Web服务发送http请求以从中获取信息
我的函数以使数据正常工作并获得数据确定
我需要在计时器中添加该功能以保持数据到应用程序的问题
当我在计时器中添加函数时,我无法使其工作数据第一次然后它停止
我必须重新启动应用程序然后它只能第一次工作
和一些它给了我一个任务被取消
任何想法如何让它工作
I'm working in vb winform application that need to send http Request to API web service to get info from it
my Function to get the data work fine and get the data ok
the problem that I need to add that function in a timer to stay getting the data to the app
I cant make it work that way when I add the function in a timer it get the data for the first time then it stop
I have to restart the app and then it works only for the first time
and some times it gives me A "Task was Canceled"
Any Idea how to make it work
<pre> <pre> Public Async Function RunAsync() As Task
Try
Dim client As HttpClient = New HttpClient()
'client.Timeout = TimeSpan.FromMinutes(600)
Dim Auth As New Auth()
Auth.UserName = "UserName"
Auth.Password = "Password"
Dim wait As New WaitRequest()
wait.ServiceId = 72
wait.TerminalKey = ""
wait.SimCard = ""
wait.Auth = Auth
Dim response As HttpResponseMessage = Await client.PostAsJsonAsync("http://213.158.164.90/TopUpTest/api/getnew", wait)
Dim content As HttpContent = response.Content
'TimeSpan.FromMinutes(30)
' Get contents of page as a String.
Dim result As String = Await content.ReadAsStringAsync()
'client.Dispose()
'client.CancelPendingRequests()
'Convert the Json to Object
'Dim Invoice As List(Of Invoices) = Newtonsoft.Json.JsonConvert.DeserializeObject(Of List(Of Invoices))(result)
' If data exists, print a substring.
If result IsNot Nothing And result.Length > 50 Then
'Timer1.Enabled = False
'MsgBox(result)
frmMain.btnStartRecharge.BackColor = Color.Transparent
Dim jsonString As String = result
Dim dtJson As DataTable = ConvertJSONToDataTable(jsonString)
frmMain.InvoiceDataGridView.DataSource = dtJson
frmMain.lblMobRow.Text = frmMain.InvoiceDataGridView.RowCount
If frmMain.InvoiceDataGridView.RowCount > 1 Then
Dim Rcount As Int16 = frmMain.InvoiceDataGridView.RowCount
frmMain.InvoiceDataGridView.Rows.RemoveAt(Rcount - 2)
End If
End If
Catch ex As Exception
' Show the exception's message.
frmMain.lblmessage.Text = ex.Message
frmMain.btnStartRecharge.BackColor = Color.Red
End Try
End Function
我尝试过:
我试过
What I have tried:
I tried
'client.Timeout = TimeSpan.FromMinutes(600)
'client.Dispose()
'client.CancelPendingRequests()
推荐答案
以下(未经测试的)代码将等到发生者第一:
The following (untested) code will wait until whichever occurs first:
Dim tasks = New Task() _
{
RunAsync(),
Task.Delay(5000)
}
Task.WaitAny(tasks)
这篇关于Vb.net在计时器中添加http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!