Click here to Skip to main content
15,889,810 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

VB
<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()
Posted
Updated 6-Mar-17 2:49am

1 solution

The following (untested) code will wait until whichever occurs first:
VB
Dim tasks = New Task() _
{
    RunAsync(),
    Task.Delay(5000)
}

Task.WaitAny(tasks)
 
Share this answer
 
Comments
GalaxyMan 7-Mar-17 4:32am    
I try it it Give me this error

Additional information: Unable to cast object of type 'System.Threading.Tasks.Task[]' to type 'System.Threading.Tasks.Task'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900