报告CopyToAsync操作的进度

时间:2014-08-25 19:34:18

标签: vb.net winforms .net-4.5

有没有办法报告FileStream上的CopyToAsync操作的进度?据我所知,没有列出FileStream对象的事件,因此我无法为其添加处理程序。我发现的最佳示例涉及WebClient对象的DownloadProgressChanged / DownloadFileComplete。

For i As Int32 = 0 To strFileList.Count - 1
    Try
        Using srmSource As FileStream = File.Open(dirSource + strFileList(i), FileMode.Open)
            Using srmDestination As FileStream = File.Create(dirDestination + strFileList(i))
                Me.lblStatus.Text = "Copying file - " & strFileList(i) & "..."
                Await srmSource.CopyToAsync(srmDestination)
            End Using
        End Using
        Me.lblStatus.Text = "Copying complete!"
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
Next

1 个答案:

答案 0 :(得分:1)

以下是我使用这些链接作为参考资料的方法:

http://blogs.msdn.com/b/dotnet/archive/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis.aspx(从C#转换为VB.NET)

http://social.msdn.microsoft.com/Forums/en-US/8c121fef-ebc7-42ab-a2f8-3b5e9a6e9854/delegates-with-parameter?forum=vbide

Imports System.IO
Imports System.Net
Imports System.Threading.Tasks

Public Class frmStartup

Private Async Sub frmStartup_Load(sender As Object, e As EventArgs) Handles Me.Load

    Dim FileList As List(Of String) = GetFilesToTransfer()
    If FileList.Count > 0 Then
        UpdateLabel("Found files to transfer...")
        Me.prgTransfer.Visible = True
        Try
            Dim ProgressIndicator As Object = New Progress(Of Int32)(AddressOf ReportProgress)
            Await TransferFiles(FileList, ProgressIndicator)
            UpdateLabel("File transfer complete!")
        Catch ex As Exception
            UpdateLabel("Error transferring files!")
        Finally
            Me.prgTransfer.Visible = False
        End Try

    End If

End Sub

Private Function GetFilesToTransfer() As List(Of String)

    Dim strFilesToTransfer As List(Of String) = New List(Of String)
    strFilesToTransfer.Add("aud1.mp3")
    strFilesToTransfer.Add("aud2.mp3")
    Return strFilesToTransfer

End Function

Public Async Function TransferFiles(ByVal FileList As List(Of String), ByVal Progress As IProgress(Of Int32)) As Task

    Dim intTotal As Int32 = FileList.Count
    Dim dirSource As String = "\\source\"
    Dim dirDestination As String = "c:\destination\"

    Await Task.Run(Async Function()
                       Dim intTemp As Int32 = 0
                       For i As Int32 = 0 To FileList.Count - 1
                           UpdateLabel("Copying " & FileList(i) & "...")

                           Using srmSource As FileStream = File.Open(dirSource + FileList(i), FileMode.Open)
                               Using srmDestination As FileStream = File.Create(dirDestination + FileList(i))
                                   Await srmSource.CopyToAsync(srmDestination)
                               End Using
                           End Using

                           intTemp += 1
                           If Progress IsNot Nothing Then
                               Progress.Report((intTemp * 100 / intTotal))
                           End If
                       Next
                   End Function)
End Function

Private Delegate Sub UpdateLabelInvoker(ByVal LabelText As String)
Private Sub UpdateLabel(ByVal LabelText As String)
    If Me.lblStatus.InvokeRequired Then
        Me.lblStatus.Invoke(New UpdateLabelInvoker(AddressOf UpdateLabel), LabelText)
    Else
        Me.lblStatus.Text = LabelText
    End If
End Sub

Private Sub ReportProgress(ByVal Value As Int32)
    Me.prgTransfer.Value = Value
End Sub

End Class