How to avoid form freezing RRS feed

  • Question

  • 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 :

    ProgressBar1.Maximum = Conversions.ToString(ListBox1.Items.Count)
            total.Text = Conversions.ToString(ListBox1.Items.Count)
            If Not ProgressBar1.Value = ProgressBar1.Maximum Then
                    ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
                    TextBox1.Text = ListBox1.SelectedItem.ToString
                Catch ex As Exception
                End Try
                    Dim scanbox As New TextBox
                    Dim read As String = My.Computer.FileSystem.ReadAllText("viruslist.txt")
                    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)
                    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))
                    If scanbox.Text.Contains(buff.ToString) Then
                    End If
                Catch ex As Exception
                End Try
                MsgBox("Finished Scanning Folder!")
                If ListBox1.Items.Count = 0 Then
                    MsgBox("No Threats were detected, Scan Window will close!", MsgBoxStyle.Information)
                End If
            End If

    I also include the code for the "Start scan" button which the user will press to begin process:

     FolderBrowserDialog1.SelectedPath = ("C:/Windows")
            FolderBrowserDialog2.SelectedPath = ("D:/")
                    For Each strDir As String In System.IO.Directory.GetDirectories(FolderBrowserDialog1.SelectedPath)
                        For Each strFile As String In System.IO.Directory.GetFiles(strDir)
                        total.Text = strFile
                Catch ex As Exception
            End Try

    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!

    • Edited by John2000k Friday, May 1, 2020 6:13 PM minor mistakes
    Friday, May 1, 2020 6:07 PM

All replies

  • ...... the form becomes unresponsive and crashes afterwards.


    You need to fix the issue that is causing the crash .................. then fix the issue causing the unresponsiveness (BackGroundWorker?)


    Another possibility .............  explain the 'crashes' in a more understandable way ................ in which case,maybe someone here will assist.

    Regards Les, Livingston, Scotland

    • Edited by leshay Friday, May 1, 2020 6:57 PM
    Friday, May 1, 2020 6:56 PM
  • I might not understand VB.Net well enough but it appears to me that you are catching exceptions and ignoring them. If that is what your program is doing then that is a big mistake. First, before asking for help in a forum, do something to report the exceptions. If there is an exception occurring and being ignored then of course you will get a crash that is more difficult to figure out.

    I do not understand freeze and crash. That is not possible. If it freezes then it cannot crash. Unless you mean it is crashing first then freezing.

    Whenever you say crash, tell us what the error code is. Oh, except do you mean that freeze and crash are the same thing?

    Sam Hobbs

    Friday, May 1, 2020 6:57 PM
  • The above code works well if not using the Backgroundworker, but by using a Timer and a button, along with the rest of the tools from the toolbox(listbox, etc).

    The problem is, when I add more MD5 hashes to the text file (which is used to detect malicious items), the application is blocking itself , becoming unresponsive.

    Not sure why it is causing this error in the first place.

    It might be the issue when reading the text file with the hashes, not sure though , or if it will fix when using a backgroundworker, allowing to run the process in a separate thread besides the UI. If someone could help, that would be great.

    Dim read As String = My.Computer.FileSystem.ReadAllText("viruslist.txt")

    Friday, May 1, 2020 7:02 PM
  • Some tips which may be new to you and will take time to learn

    • Never use Sleep (use async Task.Delay(x) were x is milliseconds.
    • Consider using async task
    • When using async task never use a procedure/sub, always uses functions that return a task else exceptions can get unnoticed.
    • In tangent with CancellationToken (learn how to allow reentry for running task and cancellations more than once)

    I don't have any VB code samples, only C#. If you care to look the following repository shows how to do basic and intermediate level async programming with files.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Friday, May 1, 2020 10:01 PM
  • Thanks for the reply. I've looked into the links suggested.

    Since my software would use a timer to to its work, I'm not sure if a BackgroundWorker does support the timer's tick event. I've found no info on the web for using a backgroundworker with a timer.

    I've attached a link to my project source code with the work I've done so far, if someone's interested:


    • Edited by John2000k Saturday, May 2, 2020 5:26 AM
    Saturday, May 2, 2020 5:25 AM
  • Hi John2000k,

    Thanks for your feedback.

    According to your question, I have a question to confirm with you.

    >>the application is blocking itself

    Could you provide more details about which line of the code block your application? It will help us to analyze your problem.

    Besides, as Kareninstructor suggested, you can consider using asynchronous programming in your application to prevent winforms UI block.

    Here's a document you can refer to:

    Asynchronous programming with Async and Await (Visual Basic)

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 4, 2020 6:45 AM
    • Hello. Thanks for your answer.

    Well, it does not give me an error while debugging, I'll start the "scan" process, in which it will enumerate the files needed to be checked, displaying in a label the path being scanned (just like a real antivirus does) and it reads a text file which is the database containing virus md5 hashes used to detect malware items.

    The bigger this text file is(database with md5 hashes) , the more unresponsive the UI becomes. It works very good with small text files(less hashes), but that's really pointless and inefficient.

    • Edited by John2000k Wednesday, May 6, 2020 4:50 PM
    Wednesday, May 6, 2020 4:49 PM
  • Somebody wrote this already in this thread, you're using the new way of on error resume in your code. (Try, catch, ignore, end try)

    With one piece of this kind of ostrich code, you don't know what happening and where the lag can be.


    Wednesday, May 6, 2020 6:05 PM