大佬教程收集整理的这篇文章主要介绍了“System.Threading.Tasks.Task(Of String)”类型的值无法转换为“String”。在 VB.Net 中,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试 Asyn 调用,但在对结果进行拆箱时遇到问题。这是我得到的错误信息:“System.Threading.Tasks.Task(Of String)”类型的值无法转换为“String”。 有人可以帮助实现这一目标。这是我使用的代码:
Private Sub Download_Click(sender As Object,e As EventArgs) Handles button1.Click
'GET DOWNLOAD URL
Dim download_url As String = Download_httpClIEnt(space_ID,file_id)
'ACCESS URL THROUGH Google Chrome
Dim proc As New processstarTinfo()
proc.filename = "C:\windows\System32\cmd.exe"
proc.Arguments = "/c start Chrome " + download_url
Process.Start(proC)
End Sub
Public Async Function Download_httpClIEnt(ByVal spacEID As String,filEID As String) As Task(Of String)
Dim cookieContainer As New cookieContainer
Dim httpclienthandler As New httpclienthandler
httpclienthandler.AllowautoRedirect = True
httpclienthandler.Usecookies = True
httpclienthandler.cookieContainer = cookieContainer
Dim httpClIEnt As New httpClIEnt(httpclienthandler)
'GET LOGIN CREDENTIALS
Dim login_url As String = host + "/common/Authentication.Json"
Dim login_parameter As String = "?ID=" + user_iD + "&pw=" + password
Dim login_response As httpResponsemessage = Await httpClIEnt.GetAsync(login_url + login_parameter)
Dim login_contents As String = Await login_response.Content.ReadAsStringAsync()
'messageBox.Show(login_contents)
'GET DOWNLOAD ID
Dim download_url As String = host + "/contentsmanagement/SingleContentsDownloadAPI.Json"
Dim download_param As String = "?filEID=" + file_id + "&spacEID=" + space_ID + "&type=alive"
Dim download_response As httpResponsemessage = Await httpClIEnt.GetAsync(download_url + download_param)
Dim download_contents As String = Await download_response.Content.ReadAsStringAsync()
Dim downloadID As String = nothing
Dim map As Dictionary(Of String,String)
map = JsonConvert.DeserializeObject(Of Dictionary(Of String,String))(download_contents)
map.TryGetValue("ID",downloadID)
'messageBox.Show(downloadID)
'set DOWNLOAD URL
Dim url As String = host + "/contentsmanagement/ContentsDownloadAPI.Json?ID=" + downloadID
Return url
End Function
当函数声明为 Async
并返回 Task(Of T)
时,您需要在调用它时Await
以获取 T
生成的 Task
对象{1}},即:
Dim download_url As String = Download_httpClient(space_id,file_id)
需要改成这样:
Dim download_url As String = Await Download_httpClient(space_id,file_id)
您只能在本身声明为 Await
的方法中使用 Async
。这意味着:
Private Sub Download_Click(sender As Object,e As EventArgs) Handles Button1.Click
需要改成这样:
Private Async Sub Download_Click(sender As Object,e As EventArgs) Handles Button1.Click
请注意,您应该只将 Async Sub
用于事件处理程序,否则使用 Async Function
。
我通常会推荐一些关于 Async
/Await
的阅读,但它是一个有点高级的主题。作为一般规则,如果您需要调用现有的 Async
函数,例如httpClient.GetAsync
,那么您也需要创建自己的方法 Async
。
如果您有这样的 Sub
:
Private Sub SomeMethod()
你这样称呼它:
SomeMethod()
然后就变成这样了:
Private Async Function SomeMethodAsync() As Task
还有这个:
Await SomeMethodAsync()
异常是一个事件处理程序,我已经在上面演示过。它们必须是 Sub
并且您不能直接调用它们。
同样,如果您有这样的 Function
:
Private Function SomeMethod() As T
你这样称呼它:
Dim result As T = SomeMethod()
然后就变成这样了:
Private Async Function SomeMethodAsync() As Task(Of T)
还有这个:
Dim result As T = Await SomeMethodAsync()
如果您有一个不是 Async
的方法,并且您想在其中调用 Async
方法,则必须使您的方法 Async
并使用 Await
,然后创建任何调用它的方法 Async
并在其中使用 Await
,并在链上向上直到到达事件处理程序。这就是为什么通常建议,如果您打算使用 Async
,您几乎可以在任何地方使用它。
如果让 Async
的所有内容都过于繁琐,那么您可以同步调用异步方法并获取返回的 Task
的结果,例如这个:
Dim download_url As String = Download_httpClient(space_id,file_id)
变成这样:
Dim download_url_task As Task(Of String) = Download_httpClient(space_id,file_id)
Dim download_url As String = download_url_task.Result
或者只是这个:
Dim download_url As String = Download_httpClient(space_id,file_id).Result
以上是大佬教程为你收集整理的“System.Threading.Tasks.Task(Of String)”类型的值无法转换为“String”。在 VB.Net 中全部内容,希望文章能够帮你解决“System.Threading.Tasks.Task(Of String)”类型的值无法转换为“String”。在 VB.Net 中所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。