vb.net progress bar value throws ArgumentOutOfRange exception while uploading a file using await async

222 Views Asked by At

i can not update a ProgressBar value,i tried also Convert.ToInt32(Bytes as long).But,it does not worked. i'm using Progress (of integer).

'Button OnClick



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

i am stucked into this,can't know what i missed.I want to progress Bytes which is transferred to target folder in my progressbar.for example, the file size is 1gb(1073741824 bytes) then how can i set Progressbar maximum value=1073741824 and progressbar value + =transferredbytes.

1

There are 1 best solutions below

3
On

It seems that when you want to calculate the Maximum value, you've token into account the value of the TotalSize through the Select-Case mechanism and hence, scaled it based on its range. But in the ReportProgress, the myInt input integer is directly added to the progressbar value. I think your Select-Case should be implemented in the ReportProgress as well.

Edit 1:

Let's assume that TotalSize = 109951162777. Right? Hence, Byte = 1 and the progressbar's maximum value is equal to 1 (i.e., 1 TB). Then, in the ReportProgress function you must first divide myInt (which is in bytes) by 109951162777 to make it a TB value (e.g., 0.5 TB) and then update the progressbar's value. If you do not do so, myInt will exceed the int32 limit and errors occur. Am I right? Therefore, you have to know that which case is selected in the Select-Case statement and the TotalSize is divided by which number? I recommend to modify the Button1_Click function as:

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

and the ReportProgress as follows:

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

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

Hope this solves the problem.