Vb.net 异步和等待,为什么有两个返回值

Vb.net 异步和等待,为什么有两个返回值,vb.net,async-await,Vb.net,Async Await,我正试着让我的头脑转向Async并等待。进展顺利,但有一件事我想澄清一下,为什么我的方法中有两个返回语句。我真的在寻找一个关于幕后发生的事情的解释 我将在下面发布完整的代码,因为它只有大约80行。我说的是中央方法AllSubfolderFiles,它既有返回计数器又有返回dirsFraction。这些到底是怎么回事 基本上,它是一个WinForm应用程序,迭代子文件夹的所有文件,为每个迭代的子文件夹更新ProgressBar Imports System.IO Public Class frm

我正试着让我的头脑转向
Async并等待
。进展顺利,但有一件事我想澄清一下,为什么我的方法中有两个返回语句。我真的在寻找一个关于幕后发生的事情的解释

我将在下面发布完整的代码,因为它只有大约80行。我说的是中央方法
AllSubfolderFiles
,它既有
返回计数器
又有
返回dirsFraction
。这些到底是怎么回事

基本上,它是一个WinForm应用程序,迭代子文件夹的所有文件,为每个迭代的子文件夹更新ProgressBar

Imports System.IO

Public Class frmAsyncProgress

    Private Sub frmAsyncProgress_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        barFileProgress.Minimum = 0
        barFileProgress.Maximum = 100
        btnCancel.Enabled = False
    End Sub

    Private Async Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        If String.IsNullOrWhiteSpace(txtPath.Text) Then
            MessageBox.Show("Provide a location first.", "Location")
            Exit Sub
        End If
        Dim sLocation As String = txtPath.Text.Trim()
        If Not Directory.Exists(sLocation) Then
            MessageBox.Show("Directory doesn't exist.", "Location")
            Exit Sub
        End If

        Dim progressIndicator = New Progress(Of Integer)(AddressOf UpdateProgress)
        btnStart.Enabled = False
        btnCancel.Enabled = True
        lblPercent.Text = "0%"

        Dim allFiles As Integer = Await AllSubfolderFiles(sLocation, progressIndicator)
        Debug.WriteLine(allFiles.ToString())        'the number of subfolders iterated
        btnStart.Enabled = True
        btnCancel.Enabled = False
    End Sub

    Private Async Function AllSubfolderFiles(location As String, progress As IProgress(Of Integer)) As Task(Of Integer)
        Dim dirsTotal As Integer = Directory.GetDirectories(location).Length
        Dim dirsFraction As Integer = Await Task(Of Integer).Run(Function()
                                                                     Dim counter As Integer = 0
                                                                     For Each subDir As String In Directory.GetDirectories(location)
                                                                         SubfolderFiles(subDir)
                                                                         counter += 1
                                                                         If progress IsNot Nothing Then
                                                                             progress.Report(counter * 100 / dirsTotal)
                                                                         End If
                                                                     Next

                                                                     Return counter
                                                                 End Function)
        Return dirsFraction
    End Function

    Private Sub UpdateProgress(value As Integer)
        barFileProgress.Value = value
        lblPercent.Text = (value / 100).ToString("#0.##%")
    End Sub

    Private Sub SubfolderFiles(location As String)
        'source: http://stackoverflow.com/questions/16237291/visual-basic-2010-continue-on-error-unauthorizedaccessexception#answer-16237749

        Dim paths = New Queue(Of String)()
        Dim fileNames = New List(Of String)()

        paths.Enqueue(location)

        While paths.Count > 0
            Dim sDir = paths.Dequeue()

            Try
                Dim files = Directory.GetFiles(sDir)
                For Each file As String In Directory.GetFiles(sDir)
                    fileNames.Add(file)
                Next

                For Each subDir As String In Directory.GetDirectories(sDir)
                    paths.Enqueue(subDir)
                Next
            Catch ex As UnauthorizedAccessException
                ' log the exception or ignore it
                Debug.WriteLine("Directory {0}  could not be accessed!", sDir)
            Catch ex As Exception
                ' log the exception or ...
                Throw
            End Try
        End While
        'could return fileNames collection
    End Sub
End Class
我的评估是,
计数器
被返回,然后作为
dirsFraction
封送回UI线程,但我不相信我尝试的解释


在您调用的
任务的
所有子文件夹文件
函数中。运行
并传入一个匿名函数,该函数使用
返回计数器返回
AllSubfolderFiles
等待调用结果,然后返回
returndirsfraction


因此,在同一个函数中有两个返回,因为在原始函数中有一个匿名函数。您可以将该函数移到它自己的命名函数中,这样可以更清楚地看到这里有两个不同的函数。

在您调用的
任务的
所有子文件夹文件
函数中。运行
并传入一个匿名函数,该函数返回
返回计数器
AllSubfolderFiles
等待调用结果,然后返回
returndirsfraction


因此,在同一个函数中有两个返回,因为在原始函数中有一个匿名函数。您可以将该函数移到它自己的命名函数中,这将使这里有两个不同的函数更加清晰。

非常感谢,这很有意义(我就快到了;)。非常感谢,这很有意义(我就快到了;))。