程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了“System.Threading.Tasks.Task(Of String)”类型的值无法转换为“String”。在 VB.Net 中大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决“System.Threading.Tasks.Task(Of String)”类型的值无法转换为“String”。在 VB.Net 中?

开发过程中遇到“System.Threading.Tasks.Task(Of String)”类型的值无法转换为“String”。在 VB.Net 中的问题如何解决?下面主要结合日常开发的经验,给出你关于“System.Threading.Tasks.Task(Of String)”类型的值无法转换为“String”。在 VB.Net 中的解决方法建议,希望对你解决“System.Threading.Tasks.Task(Of String)”类型的值无法转换为“String”。在 VB.Net 中有所启发或帮助;

我正在尝试 Asyn 调用,但在对结果进行拆箱时遇到问题。这是我得到的错误信息:“System.Threading.Tasks.Task(Of String)”类型的值无法转换为“String”。 有人可以帮助实现这一目标。这是我使用的代码:

@H_674_7@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
@H_197_9@ 

解决方法

当函数声明为 @H_674_7@Async 并返回 @H_674_7@Task(Of T) 时,您需要在调用它时@H_674_7@Await 以获取 @H_674_7@T 生成的 @H_674_7@Task 对象{1}},即:

@H_674_7@Dim download_url As String = Download_httpClient(space_id,file_id)
@H_197_9@

需要改成这样:

@H_674_7@Dim download_url As String = Await Download_httpClient(space_id,file_id)
@H_197_9@

您只能在本身声明为 @H_674_7@Await 的方法中使用 @H_674_7@Async。这意味着:

@H_674_7@Private Sub Download_Click(sender As Object,e As EventArgs) Handles Button1.Click
@H_197_9@

需要改成这样:

@H_674_7@Private Async Sub Download_Click(sender As Object,e As EventArgs) Handles Button1.Click
@H_197_9@

请注意,您应该只将 @H_674_7@Async Sub 用于事件处理程序,否则使用 @H_674_7@Async Function。

我通常会推荐一些关于 @H_674_7@Async/@H_674_7@Await 的阅读,但它是一个有点高级的主题。作为一般规则,如果您需要调用现有的 @H_674_7@Async 函数,例如@H_674_7@httpClient.GetAsync,那么您也需要创建自己的方法 @H_674_7@Async。

如果您有这样的 @H_674_7@Sub:

@H_674_7@Private Sub SomeMethod()
@H_197_9@

你这样称呼它:

@H_674_7@SomeMethod()
@H_197_9@

然后就变成这样了:

@H_674_7@Private Async Function SomeMethodAsync() As Task
@H_197_9@

还有这个:

@H_674_7@Await SomeMethodAsync()
@H_197_9@

异常是一个事件处理程序,我已经在上面演示过。它们必须是 @H_674_7@Sub 并且您不能直接调用它们。

同样,如果您有这样的 @H_674_7@Function:

@H_674_7@Private Function SomeMethod() As T
@H_197_9@

你这样称呼它:

@H_674_7@Dim result As T = SomeMethod()
@H_197_9@

然后就变成这样了:

@H_674_7@Private Async Function SomeMethodAsync() As Task(Of T)
@H_197_9@

还有这个:

@H_674_7@Dim result As T = Await SomeMethodAsync()
@H_197_9@

如果您有一个不是 @H_674_7@Async 的方法,并且您想在其中调用 @H_674_7@Async 方法,则必须使您的方法 @H_674_7@Async 并使用 @H_674_7@Await,然后创建任何调用它的方法 @H_674_7@Async 并在其中使用 @H_674_7@Await,并在链上向上直到到达事件处理程序。这就是为什么通常建议,如果您打算使用 @H_674_7@Async,您几乎可以在任何地方使用它。

如果让 @H_674_7@Async 的所有内容都过于繁琐,那么您可以同步调用异步方法并获取返回的 @H_674_7@Task 的结果,例如这个:

@H_674_7@Dim download_url As String = Download_httpClient(space_id,file_id)
@H_197_9@

变成这样:

@H_674_7@Dim download_url_task As Task(Of String) = Download_httpClient(space_id,file_id)
Dim download_url As String = download_url_task.Result
@H_197_9@

或者只是这个:

@H_674_7@Dim download_url As String = Download_httpClient(space_id,file_id).Result
@H_197_9@

大佬总结

以上是大佬教程为你收集整理的“System.Threading.Tasks.Task(Of String)”类型的值无法转换为“String”。在 VB.Net 中全部内容,希望文章能够帮你解决“System.Threading.Tasks.Task(Of String)”类型的值无法转换为“String”。在 VB.Net 中所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。