Home > Mobile >  VB.net Asynchronous call to Url with no response needed
VB.net Asynchronous call to Url with no response needed

Time:07-06

I have a VB.Net page that needs to submit data to a url after a user clicks a button. I don't need any data back from the url, I just need to pass parameters to it and allow the user to continue to the next step without waiting for the url to do it's thing.

I've seen some similar posts for c# using UploadStringTaskAsync, but haven't been able to find a corresponding method for VB.net. https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.uploadstringtaskasync?view=net-6.0

I believe I can call the Async method from my existing nonasync methods as I don't need the response back. However, if there is a more elegant approach to do this please let me know.

Update with current code attempting to use thread:

Sub Page_Load(sender As Object, e As EventArgs)
     If Not IsPostBack Then
          Dim thread As New Thread(AddressOf testSub)
          thread.Start()
     End If
End Sub

Sub testSub
     Using WC As New WebClient  WC.UploadString("https://someurl.com?parameter1=testing&parameter2=anothertest", "SOMEDATA")
     End Using
End Sub

This runs but unfortunately does not appear to handle any of the parameters. When I post the url directly in the browser it runs as expected. I don't need to send any data other than the querystring as well so I'm not sure if that's breaking the uploadstring. However, when I run it via debugger I don't see any errors so long as I populate that string for the data with a value.

I might be misunderstanding though when the await call is needed. While I don't need any data back, the external url can take up to 5 minutes to process. I'm wondering if it's taking too long and timing out after that thread is started.

CodePudding user response:

You could run it in its own thread.

Imports System.Net
Imports System.Threading

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        ' Create a thread to run this in the background
        Dim t As New Thread(
            Sub()
                Using WC As New WebClient
                    WC.UploadString("https://YOURURL", "YOURDATA")
                End Using

            End Sub
        )
        ' Start the thread
        t.Start()

    End Sub


End Class
  • Related