1

我无法更新 ProgressBar 值,我也尝试过 Convert.ToInt32(Bytes as long)。但是,它不起作用。我正在使用进度(整数)。

'按钮点击



Public Shared s3client As AmazonS3Client
Public Shared myProgress As Progress(Of Integer)  
Public Shared Bytes As Double
Public Shared myProgress As Progress(Of Integer)
Public Shared bucketName As String = "S3BucketName"

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button2.Click

 Dim AllowedFiles As List(Of String) = New List(Of String)

            Try
                Dim TheSize As Long
                Dim TotalSize As Long
                For Each file In AllowedFiles
                    TheSize = Long.Parse((My.Computer.FileSystem.GetFileInfo(file).Length))
                    TotalSize += TheSize
                Next

                Select Case TotalSize
                    Case Is >= 1099511627776
                        Bytes = CDbl(TotalSize / 1099511627776) 'TB
                    Case 1073741824 To 1099511627775
                        Bytes = CDbl(TotalSize / 1073741824) 'GB
                    Case 1048576 To 1073741823
                        Bytes = CDbl(TotalSize / 1048576) 'MB
                    Case 1024 To 1048575
                        Bytes = CDbl(TotalSize / 1024) 'KB
                    Case 0 To 1023
                        Bytes = TotalSize ' bytes
                    Case Else
                        Bytes = 0
                        'Return ""
                End Select

                ProgForm2.CPBar1.Value = 0
                ProgForm2.CPBar1.Minimum = 0
                ProgForm2.CPBar1.Maximum = Convert.ToInt32(Bytes)

                Dim result As DialogResult = MessageBox.Show("Selected  " & TotalFiles & " files have " & CalculateSize.ToString & "" & SizeType, "in total Size", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)

                If result = DialogResult.OK And TextBox1.Text IsNot "" = True Then

                    myProgress = New Progress(Of Integer)(AddressOf ReportProgress)
                         Foldername=Textbox1.Text
                        For Each file In AllowedFiles
                            Try
                                ProgForm2.Show()
                                Await AddFileToRootFolderAsync(file, bucketName, Foldername, myProgress)
                                TheSize = Long.Parse(My.Computer.FileSystem.GetFileInfo(file).Length)

                            Catch ex As Exception
                                MessageBox.Show(ex.Message)
                            End Try

                            ProgForm2.CPBar1.Text = ProgForm2.CPBar1.Value.ToString + "/" + Form2.CPBar1.Maximum.ToString
                        Next

                        ProgForm2.CPBar1.Value = ProgForm2.CPBar1.Maximum
                        If ProgForm2.CPBar1.Value = ProgForm2.CPBar1.Maximum Then
                            ProgForm2.CPBar1.Text = "Task Completed"
                            ProgForm2.Button1.Show()
                        End If
                 Else
                     Exit Sub
                 End if

End Sub

'file uploading function

Public Async Function AddFileToFolderAsync(FileName As String, bucketName As String, folderName As String, ByVal myProgress As IProgress(Of Integer)) As Task


        Try
                If AmazonS3Util.DoesS3BucketExistV2(s3client, bucketName) Then

                    Dim Checkresult = FolderCheck(bucketName, folderName) /'Folder Exist or Not
                    If Checkresult = True Then
                        Dim keyname As String = "" 'destination path(s3 bucket folder)
                        Dim filepath As String = FileName 'current file's local fullpath
                        Dim fname As String = Path.GetFileName(FileName) 'filename
                        If Not folderName.EndsWith("/") Then
                            keyname += folderName & "/"
                            keyname += fname  'bucket's target folder /fname (eg:folder/subfolder/file.mp4)
                        Else
                            keyname += fname  'bucket's target folder /fname (eg:folder/subfolder/file.mp4)
                        End If

                        Dim fileTransferUtility = New TransferUtility(s3client)
                        Dim fileTransferUtilityRequest = New TransferUtilityUploadRequest With {
                            .BucketName = bucketName,
                            .FilePath = filepath,
                            .StorageClass = S3StorageClass.Standard,
                            .ServerSideEncryptionMethod = ServerSideEncryptionMethod.None,
                            .PartSize = 6291456,
                            .Key = keyname,
                            .ContentType = "*.*"}
                        AddHandler fileTransferUtilityRequest.UploadProgressEvent,
                         Sub(sender As Object, e As UploadProgressArgs)
                             Dim percent As Integer = Convert.ToInt32(e.TransferredBytes)  //e.TransferredBytes as long
                             myProgress.Report(percent)
                         End Sub
                        Await fileTransferUtility.UploadAsync(fileTransferUtilityRequest)
                    Else
                        MessageBox.Show(folderName + " folder does not exist")
                    End If
                Else
                    MessageBox.Show(bucketName + " Bucket does not exist")
                End If

            Catch ex As AmazonS3Exception
            MessageBox.Show(ex.Message + " Upload task canceled.")

        Catch ex As Exception
            MessageBox.Show(ex.Message + " Upload task canceled.")

        End Try

    End Function
Public Sub ReportProgress(ByVal myInt As Integer)

    Form2.CPBar1.Value += myInt

    Form2.CPBar1.Text = Form2.CPBar1.Value.ToString + "/" + Form2.CPBar1.Maximum.ToString 

End Sub

我被困在这里,不知道我错过了什么。我想在我的进度条中传输传输到目标文件夹的字节。例如,文件大小为 1gb(1073741824 字节)那么我该如何设置进度条最大值= 1073741824 和进度条值 + =transferredbytes。

4

1 回答 1

1

似乎当您想要计算最大值时,您已经通过 Select-Case 机制考虑了 TotalSize 的值,因此根据其范围对其进行了缩放。但是在 ReportProgress 中,myInt 输入整数是直接添加到进度条值的。我认为您的 Select-Case 也应该在 ReportProgress 中实现。

编辑1:

让我们假设TotalSize = 109951162777. 正确的?因此,Byte = 1进度条的最大值等于 1(即 1 TB)。然后,在ReportProgress函数中,您必须首先将 myInt(以字节为单位)除以 109951162777 使其成为 TB 值(例如,0.5 TB),然后更新进度条的值。如果不这样做,myInt 将超过 int32 限制并发生错误。我对吗?因此,您必须知道Select-Case语句中选择了哪种情况,TotalSize 除以哪个数字?我建议将Button1_Click功能修改为:

Dim divider as long
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button2.Click

 Dim AllowedFiles As List(Of String) = New List(Of String)

            Try
                Dim TheSize As Long
                Dim TotalSize As Long
                For Each file In AllowedFiles
                    TheSize = Long.Parse((My.Computer.FileSystem.GetFileInfo(file).Length))
                    TotalSize += TheSize
                Next

                Select Case TotalSize
                    Case Is >= 1099511627776
       divider = 1099511627776 'TB                        
                    Case 1073741824 To 1099511627775
                        divider = 1073741824 'GB
                    Case 1048576 To 1073741823
                        divider = 1048576 'MB
                    Case 1024 To 1048575
                        divider = 1024 'KB
                    Case 0 To 1023
                        divider = 1  ' bytes
                    Case Else
                        Bytes = 0
       divider = 1
                        'Return ""
                End Select

                Bytes = CDbl(TotalSize / divider)  'always between 0 and 1

                ProgForm2.CPBar1.Value = 0
                ProgForm2.CPBar1.Minimum = 0
                ProgForm2.CPBar1.Maximum = Convert.ToInt32(Bytes)

                Dim result As DialogResult = MessageBox.Show("Selected  " & TotalFiles & " files have " & CalculateSize.ToString & "" & SizeType, "in total Size", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)

                If result = DialogResult.OK And TextBox1.Text IsNot "" = True Then

                    myProgress = New Progress(Of Integer)(AddressOf ReportProgress)
                         Foldername=Textbox1.Text
                        For Each file In AllowedFiles
                            Try
                                ProgForm2.Show()
                                Await AddFileToRootFolderAsync(file, bucketName, Foldername, myProgress)
                                TheSize = Long.Parse(My.Computer.FileSystem.GetFileInfo(file).Length)

                            Catch ex As Exception
                                MessageBox.Show(ex.Message)
                            End Try

                            ProgForm2.CPBar1.Text = ProgForm2.CPBar1.Value.ToString + "/" + Form2.CPBar1.Maximum.ToString
                        Next

                        ProgForm2.CPBar1.Value = ProgForm2.CPBar1.Maximum
                        If ProgForm2.CPBar1.Value = ProgForm2.CPBar1.Maximum Then
                            ProgForm2.CPBar1.Text = "Task Completed"
                            ProgForm2.Button1.Show()
                        End If
                 Else
                     Exit Sub
                 End if

End Sub

如下ReportProgress

    Form2.CPBar1.Value += myInt / divider ‘to normalize myInt to [0,1]

    Form2.CPBar1.Text = Form2.CPBar1.Value.ToString + "/" + Form2.CPBar1.Maximum.ToString 

希望这能解决问题。

于 2020-05-26T05:56:29.400 回答