Partage
  • Partager sur Facebook
  • Partager sur Twitter

L'opération arithmétique a provoqué un dépassement

Sujet résolu
    4 mai 2018 à 16:53:58

    Bonjour,

    Je possède un opensource et je souhaite l'integre a mon projet seul problème il y a une erreur. Je pense que c'est un problème de 32/64Bits.

    Quand je lance le téléchargement il se bloque systématiquement vers 21 000 ko avec retour sur visual m'indiquant comme erreur celle ci.

    L'exception System.OverflowException n'a pas été gérée par le code utilisateur
      HResult=-2146233066
      Message=L'opération arithmétique a provoqué un dépassement de capacité.
      Source=SimpleDownloadFile
      StackTrace:
           à SimpleDownloadFile.mainForm.BackgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) dans D:\Users\Théo\Downloads\4\mainForm.vb:ligne 136
           à System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
           à System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
      InnerException: 
    

    J'ai contacter l'editeur il y a 1 semaine mais aucune réponse faut dire que son projet date de 10 ans.

    Voici le code :

    Imports System.Net
    Public Class mainForm
    
        Dim whereToSave As String 'Where the program save the file
    
        Delegate Sub ChangeTextsSafe(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
        Delegate Sub DownloadCompleteSafe(ByVal cancelled As Boolean)
    
        Public Sub DownloadComplete(ByVal cancelled As Boolean)
            Me.txtFileName.Enabled = True
            Me.btnDownload.Enabled = True
            Me.btnCancel.Enabled = False
    
            If cancelled Then
    
                Me.Label4.Text = "Cancelled"
    
                MessageBox.Show("Download aborted", "Aborted", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
    
            Else
                Me.Label4.Text = "Successfully downloaded"
    
                MessageBox.Show("Successfully downloaded!", "All OK", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
    
            End If
    
            Me.ProgressBar1.Value = 0
            Me.Label5.Text = "Downloading: "
            Me.Label6.Text = "Save to: "
            Me.Label3.Text = "File size: "
            Me.Label2.Text = "Download speed: "
            Me.Label4.Text = ""
    
        End Sub
    
        Public Sub ChangeTexts(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
    
            Me.Label3.Text = "File Size: " & Math.Round((length / 1024), 2) & " KB"
    
            Me.Label5.Text = "Downloading: " & Me.txtFileName.Text
    
            Me.Label4.Text = "Downloaded " & Math.Round((position / 1024), 2) & " KB of " & Math.Round((length / 1024), 2) & "KB (" & Me.ProgressBar1.Value & "%)"
    
            If speed = -1 Then
                Me.Label2.Text = "Speed: calculating..."
            Else
                Me.Label2.Text = "Speed: " & Math.Round((speed / 1024), 2) & " KB/s"
            End If
    
            Me.ProgressBar1.Value = percent
    
    
        End Sub
    
        Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
    
            If Me.txtFileName.Text <> "" AndAlso Me.txtFileName.Text.StartsWith("http://") Then
    
    
                Me.SaveFileDialog1.FileName = Me.txtFileName.Text.Split("/"c)(Me.txtFileName.Text.Split("/"c).Length - 1)
    
                If Me.SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    
                    Me.whereToSave = Me.SaveFileDialog1.FileName
    
                    Me.SaveFileDialog1.FileName = ""
    
                    Me.Label6.Text = "Save to: " & Me.whereToSave
    
                    Me.txtFileName.Enabled = False
                    Me.btnDownload.Enabled = False
                    Me.btnCancel.Enabled = True
    
                    Me.BackgroundWorker1.RunWorkerAsync() 'Start download
    
                End If
    
            Else
    
                MessageBox.Show("Please insert valid URL for download", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    
            End If
    
        End Sub
    
        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    
            'Creating the request and getting the response
            Dim theResponse As HttpWebResponse
            Dim theRequest As HttpWebRequest
            Try 'Checks if the file exist
    
                theRequest = WebRequest.Create(Me.txtFileName.Text)
                theResponse = theRequest.GetResponse
            Catch ex As Exception
    
                MessageBox.Show("An error occurred while downloading file. Possibe causes:" & ControlChars.CrLf & _
                                "1) File doesn't exist" & ControlChars.CrLf & _
                                "2) Remote server error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
                Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
    
                Me.Invoke(cancelDelegate, True)
    
                Exit Sub
            End Try
            Dim length As Long = theResponse.ContentLength 'Size of the response (in bytes)
    
            Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
            Me.Invoke(safedelegate, length, 0, 0, 0) 'Invoke the TreadsafeDelegate
    
            Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create)
    
            'Replacement for Stream.Position (webResponse stream doesn't support seek)
            Dim nRead As Integer
    
            'To calculate the download speed
            Dim speedtimer As New Stopwatch
            Dim currentspeed As Double = -1
            Dim readings As Integer = 0
    
            Do
    
                If BackgroundWorker1.CancellationPending Then 'If user abort download
                    Exit Do
                End If
    
                speedtimer.Start()
    
                Dim readBytes(4095) As Byte
                Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
    
                nRead += bytesread
                Dim percent As Short = (nRead * 100) / length
    
                Me.Invoke(safedelegate, length, nRead, percent, currentspeed)
    
                If bytesread = 0 Then Exit Do
    
                writeStream.Write(readBytes, 0, bytesread)
    
                speedtimer.Stop()
    
                readings += 1
                If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles
                    currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
                    speedtimer.Reset()
                    readings = 0
                End If
            Loop
    
            'Close the streams
            theResponse.GetResponseStream.Close()
            writeStream.Close()
    
            If Me.BackgroundWorker1.CancellationPending Then
    
                IO.File.Delete(Me.whereToSave)
    
                Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
    
                Me.Invoke(cancelDelegate, True)
    
                Exit Sub
    
            End If
    
            Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
    
            Me.Invoke(completeDelegate, False)
    
        End Sub
    
        Private Sub mainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Label4.Text = ""
        End Sub
    
        Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
            Me.BackgroundWorker1.CancelAsync() 'Send cancel request
        End Sub
    
        Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
            MessageBox.Show("Created by Carmine_XX (www.thetotalsite.it)" & ControlChars.CrLf & "To report bugs/suggestions/comments please contact me by email: pikachu31@gmail.com or in the project page on CodeProject.com", "About", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Sub
    End Class
    

    Merci

    • Partager sur Facebook
    • Partager sur Twitter
      4 mai 2018 à 17:38:22

      Salut,

      Comme indiqué dans le message d'erreur, t'as une valeur qui dépasse la limite de son type.

      C'est le calcul de nRead * 100 qui passe sans doute pas, 21 Mo * 100 ça se rapproche beaucoup de la borne haute de l'Integer (entier signé sur 32 bits).

      • Partager sur Facebook
      • Partager sur Twitter
        4 mai 2018 à 18:30:02

        Stormweaker a écrit:

        Salut,

        Comme indiqué dans le message d'erreur, t'as une valeur qui dépasse la limite de son type.

        C'est le calcul de nRead * 100 qui passe sans doute pas, 21 Mo * 100 ça se rapproche beaucoup de la borne haute de l'Integer (entier signé sur 32 bits).


        Oui mais je suis limité en connaisance, comment puis je regler cela ou avez vous une solution a me passer.

        Merci beaucoup

        • Partager sur Facebook
        • Partager sur Twitter
          6 mai 2018 à 2:31:35

          Passer sur du 64 bits devrait fonctionner, mais je ne sais pas si c'est la meilleure solution.
          • Partager sur Facebook
          • Partager sur Twitter
            8 mai 2018 à 15:46:19

            Stormweaker a écrit:

            Passer sur du 64 bits devrait fonctionner, mais je ne sais pas si c'est la meilleure solution.

            D'accord ca marche mais comment faire car je ne sais mais alors pas du tout je ne connaiser meme pas l'hisoire du passage au 64Bit.

            Merci

            • Partager sur Facebook
            • Partager sur Twitter
              12 mai 2018 à 16:20:23

              Problème résolut, j'ai finalement changer de méthode
              • Partager sur Facebook
              • Partager sur Twitter
                14 mai 2018 à 10:28:14

                Histoire de répondre, tu peux trouver les types de données en VB.NET ici : https://docs.microsoft.com/fr-fr/dotnet/visual-basic/language-reference/data-types/data-type-summary

                Pour déclarer un entier signé 64 bits il faut utiliser Long.

                • Partager sur Facebook
                • Partager sur Twitter
                  18 mai 2018 à 21:39:32

                  Stormweaker a écrit:

                  Histoire de répondre, tu peux trouver les types de données en VB.NET ici : https://docs.microsoft.com/fr-fr/dotnet/visual-basic/language-reference/data-types/data-type-summary

                  Pour déclarer un entier signé 64 bits il faut utiliser Long.


                  Avec le Long j'avais essayer mais cela ne fonctionné pas :-)
                  • Partager sur Facebook
                  • Partager sur Twitter

                  L'opération arithmétique a provoqué un dépassement

                  × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                  × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                  • Editeur
                  • Markdown