I'm doing a small antivirus software in VB.NET, which uses MD5 hashes to scan through the files from a computer. However, I noticed if I add more hashes into the text file which is used for the scan process, the form becomes unresponsive and crashes afterwards.

I've tried doing this through a backgroundworker, but I have no luck whatsoever. The software will use also a timer to do its work. I attached below a sample code of the timer 1

 ProgressBar1.Maximum = Conversions.ToString(ListBox1.Items.Count)
        total.Text = Conversions.ToString(ListBox1.Items.Count)

        If Not ProgressBar1.Value = ProgressBar1.Maximum Then
            Try

                ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
                TextBox1.Text = ListBox1.SelectedItem.ToString
            Catch ex As Exception
            End Try

            Try

                Dim scanbox As New TextBox
                Dim read As String = My.Computer.FileSystem.ReadAllText("viruslist.txt")
                ProgressBar1.Increment(1)
                Detected.Text = Conversions.ToString(ListBox2.Items.Count)
                files.Text = Conversions.ToString(ProgressBar1.Value)
                scanbox.Text = read.ToString
                Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
                Dim f As FileStream = New FileStream(ListBox1.SelectedItem, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
                f = New FileStream(ListBox1.SelectedItem, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
                md5.ComputeHash(f)
                Dim hash As Byte() = md5.Hash
                Dim buff As StringBuilder = New StringBuilder
                Dim hashByte As Byte
                For Each hashByte In hash
                    buff.Append(String.Format("{0:X2}", hashByte))
                Next

                If scanbox.Text.Contains(buff.ToString) Then

                    ListBox2.Items.Add(ListBox1.SelectedItem)
                End If
            Catch ex As Exception
            End Try
        Else
            Timer1.Stop()
            MsgBox("Finished Scanning Folder!")
            TabControl1.SelectTab(3)
            If ListBox1.Items.Count = 0 Then
                MsgBox("No Threats were detected, Scan Window will close!", MsgBoxStyle.Information)

            End If
        End If

I am also including the code for "Start scan" button:

FolderBrowserDialog1.SelectedPath = ("C:/Windows")
    FolderBrowserDialog2.SelectedPath = ("D:/")
    Try
            For Each strDir As String In System.IO.Directory.GetDirectories(FolderBrowserDialog1.SelectedPath)

                For Each strFile As String In System.IO.Directory.GetFiles(strDir)

                ListBox1.Items.Add(strFile)
                total.Text = strFile
                BackgroundWorker1.ReportProgress(strFile)
                System.Threading.Thread.Sleep(200)

            Next

            Next

        Catch ex As Exception
    End Try

    Timer1.Start()

My problem is that if I add more MD5 hashes to the text file, the application GUI will freeze and crash. I tried preventing this situation using a backgroundWorker, however I'm not sure what needs to be changed above to fit the backgroundworker doing its job properly ? Maybe something I'm doing the wrong way? The application is made with VB.NET(Visual Studio). Any help would be greatly appreciated!

I consider this to be two discussions.

  1. Speed. As in "How do I speed up my __?" As in functional areas like that text read and hash creation.
  2. Crashing. That is where YOU crash the app while running it from Visual Studio and find which line failed and then think about why. You have the onus here as you have your Visual Studio all good to go and would see what line as well as being able to set a break point on that line to inspect what you are passing to calls in that line or the math.

I'm going to chat about your first slab of code where you read the text file. These are notoriously slow so in a few apps I only read the text file when it changes. If I see the file is the same as before I read a pre-digested, pre-converted file into my array or structure. This way the slowdown on that area only happens on updates.

In parting antivirus apps have moved far beyond the method you are implementing so I take it this is not a serious attempt to write an antivirus but a learning exercise.

Is there a simplier way to speed up the process of reading the large text file containing the hashes ?

If yes, it might SOLVE the main issue.

Let me write that your code above is incomplete but even if it was YOU need to tell me about your design. To me and I'm guessing since I only see the code above I think I see your code read the text file everytime you scan a file. That would be a huge overhead.

In psuedo and again you must tell the forum your design to avoid reverse engineering and guessing I think you are getting a list of files to scan then.

  1. reading the text file.
  2. reading the scan file.
  3. doing md5 and such.
  4. if not done, go to step 1.

If you are reading the text file every time then you have lost a lot of time. Those signatures need to be in some array that you read the text file one time and one time only.

Hello. I've uploaded my project source code to Mediafire for you to see it and review it:
Click Here

commented: Sorry. That's more steps. I'll stick with what I see here. Share your thoughts as to your algorithm. +15

Ok, then how do I speed up the reading of the text file ? Is there a faster way to read the file from what I've coded ?

I noted that it looked to me you read the text file more than one time. Give me a quick overview of how you think your code works.

Since you only need to read the text file one time, speed should't be that bad. UNLESS you do it more than once.

My method for a commercial work I published and used by thousands (service industry) was to only read the text file when it was updated. Then the startup would take a bit longer as I read, parsed and created a file that could be read directly into the data space (arrays) without parsing or conversions from text to numbers.

Yes, it seems like it reads the text file over and over when it scans the system. This may be the cause of the form freezing.

How can I fix this ?

commented: Look up: "My method for a commercial work I published and used by thousands (service industry) was to only read the text file when it was updated." +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.