Adding progress bar for CRC32 checksum
-
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
-
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
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). -
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). -
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
-
DaveAuld is correct. Also, the stream.Position gives how many bytes you've read in this case.
-
DaveAuld is correct. Also, the stream.Position gives how many bytes you've read in this case.