我有一个VB。需要在用户单击按钮后将数据提交到url的网页。我不需要从url返回任何数据,我只需要将参数传递给它,并允许用户继续进行下一步,而不必等待url完成它的事情。
我已经看到了一些类似的帖子c#使用UploadStringTaskAsync,但还没有能够找到VB.net相应的方法。https://learn.microsoft.com/en us/dotnet/api/system.net.webclient.uploadstringtaskasync?view=net - 6.0
我相信我可以从我现有的非异步方法调用异步方法,因为我不需要响应回来。但是,如果有更优雅的方法,请告诉我。
更新尝试使用线程的当前代码:
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¶meter2=anothertest", "SOMEDATA")
End Using
End Sub
运行,但不幸的是似乎没有处理任何参数。当我把网址直接在浏览器中运行。我不需要发送除querystring之外的任何数据,所以我不确定这是否破坏了uploadstring。然而,当我通过调试器运行它时,我没有看到任何错误,只要我用一个值填充数据的字符串。
我可能会误解,虽然当等待调用是需要的。虽然我不需要任何数据返回,但外部url可能需要长达5分钟的处理时间。我想知道它是否需要太长时间和超时后,线程启动。
你可以在它自己的线程中运行。
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