Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Adding progress bar for CRC32 checksum

Adding progress bar for CRC32 checksum

Scheduled Pinned Locked Moved Visual Basic
question
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    johnjsm
    wrote on last edited by
    #1

    Hi, I have a public function that generates a CRC32 checksum of a file. What I am looking to add to it is a progress bar that progresses as the checksum is created. CRC32 Function

    Public Function GetCrc32(ByRef stream As System.IO.Stream) As Integer

        Dim crc32Result As Integer
        crc32Result = &HFFFFFFFF
    
        Dim buffer(BUFFER\_SIZE) As Byte
        Dim readSize As Integer = BUFFER\_SIZE
    
        Dim count As Integer = stream.Read(buffer, 0, readSize)
        Dim i As Integer
        Dim iLookup As Integer
        Dim tot As Integer = 0
        Do While (count > 0)
            For i = 0 To count - 1
                iLookup = (crc32Result And &HFF) Xor buffer(i)
                crc32Result = ((crc32Result And &HFFFFFF00) \\ &H100) And &HFFFFFF   ' nasty shr 8 with vb :/
                crc32Result = crc32Result Xor crc32Table(iLookup)
            Next i
            count = stream.Read(buffer, 0, readSize)
        Loop
    
        GetCrc32 = Not (crc32Result)
    
    End Function
    

    the function is then called when I click on a button

    Dim f As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
    crc = c32.GetCrc32(f)
    lblCRC32.Text = Hex(crc)

    How do I add a progress bar to work with the function and display the actual progress. Thanks J

    M 1 Reply Last reply
    0
    • J johnjsm

      Hi, I have a public function that generates a CRC32 checksum of a file. What I am looking to add to it is a progress bar that progresses as the checksum is created. CRC32 Function

      Public Function GetCrc32(ByRef stream As System.IO.Stream) As Integer

          Dim crc32Result As Integer
          crc32Result = &HFFFFFFFF
      
          Dim buffer(BUFFER\_SIZE) As Byte
          Dim readSize As Integer = BUFFER\_SIZE
      
          Dim count As Integer = stream.Read(buffer, 0, readSize)
          Dim i As Integer
          Dim iLookup As Integer
          Dim tot As Integer = 0
          Do While (count > 0)
              For i = 0 To count - 1
                  iLookup = (crc32Result And &HFF) Xor buffer(i)
                  crc32Result = ((crc32Result And &HFFFFFF00) \\ &H100) And &HFFFFFF   ' nasty shr 8 with vb :/
                  crc32Result = crc32Result Xor crc32Table(iLookup)
              Next i
              count = stream.Read(buffer, 0, readSize)
          Loop
      
          GetCrc32 = Not (crc32Result)
      
      End Function
      

      the function is then called when I click on a button

      Dim f As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
      crc = c32.GetCrc32(f)
      lblCRC32.Text = Hex(crc)

      How do I add a progress bar to work with the function and display the actual progress. Thanks J

      M Offline
      M Offline
      MicroVirus
      wrote on last edited by
      #2

      What you do is you get the file length (stream.Length) and then you divide the current amount of bytes processed by this value. If you multiply this fraction by 100 then you get the percent complete. All you have to do then is add a progress bar to your form, set its Min to 0, Max to 100, at the start of GetCrc32 set its Value to 0 and then after every iteration you set the progressbar Value to the percent you calculated. On a final note: you might want to do this calculating and updating the progress bar only every N iterations rather than each to avoid slowing down the function, where N is some reasonable value, like 512 or 1024 (after half and 1 KiB reads respectively).

      J 1 Reply Last reply
      0
      • M MicroVirus

        What you do is you get the file length (stream.Length) and then you divide the current amount of bytes processed by this value. If you multiply this fraction by 100 then you get the percent complete. All you have to do then is add a progress bar to your form, set its Min to 0, Max to 100, at the start of GetCrc32 set its Value to 0 and then after every iteration you set the progressbar Value to the percent you calculated. On a final note: you might want to do this calculating and updating the progress bar only every N iterations rather than each to avoid slowing down the function, where N is some reasonable value, like 512 or 1024 (after half and 1 KiB reads respectively).

        J Offline
        J Offline
        johnjsm
        wrote on last edited by
        #3

        Thank you for your answer but how do I know how many bytes are processed

        D M 2 Replies Last reply
        0
        • J johnjsm

          Thank you for your answer but how do I know how many bytes are processed

          D Offline
          D Offline
          DaveAuld
          wrote on last edited by
          #4

          Edit: sorry ignore last response, didn't read the question properly...... Anyway, You know the file length from the Stream.length as per the previous message. You also know how my bytes are being read on each iteration on the buffer. You add a totaliser, and with each byte read create the total read so far, then its simply: (total read so far / total length) * 100

          Dave Find Me On: Web|Facebook|Twitter|LinkedIn


          Folding Stats: Team CodeProject

          1 Reply Last reply
          0
          • J johnjsm

            Thank you for your answer but how do I know how many bytes are processed

            M Offline
            M Offline
            MicroVirus
            wrote on last edited by
            #5

            DaveAuld is correct. Also, the stream.Position gives how many bytes you've read in this case.

            J 1 Reply Last reply
            0
            • M MicroVirus

              DaveAuld is correct. Also, the stream.Position gives how many bytes you've read in this case.

              J Offline
              J Offline
              johnjsm
              wrote on last edited by
              #6

              Cool. Thanks guys for the help

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups