将返回的数组从异步函数转换为常规数组


我已经

有一段时间没有写过任何代码了,所以我有点生疏。我最近开始编写一个机器人,目前坚持将异步函数中的数组转换为要在另一个函数中解析的常规数组。

    Private Async Function getmsgs(ByVal num As Integer) As Task(Of Array)
        Dim msgids As Array = Nothing
        Try
            Dim msg = Await Libx.DownloadMessages(num)
'starting loop to pull each message and assign to array
            For i As Integer = msg.Count - 1 To 0 Step -1
                If (msg(i).Id > 0) Then
                    msgids(i) = msg(i).Id
                End If
            Next
'returning array
            Return msgids
        Catch ex As Exception
            Return msgids
        End Try
    End Function
    Private Sub deletemsgs(ByVal ids As Array)
        If Not ids(0) = Nothing Then
'incase library failed to retrieve messages
            Try
                For i As Integer = ids.Length - 1 To 0 Step -1
'function called per item in array
                    libx.DeleteMessages(ids(i))
                Next
            Catch ex As Exception
            End Try
        End If
    End Sub

尝试调用 deletemsgs(getmsgs(argint)) 时出现问题。我收到错误消息 Value of type 'Task(Of Array)' cannot be converted to 'Array'

我已经搜索了几个论坛,其中包含我能想到的每个相关搜索词。任何帮助将不胜感激,提前欢呼。

你需要

await async函数。

一个例子是:

Dim arr As Array() = await getmsgs(1)

这需要在async函数内部才能正常工作。

最新更新