How to avoid form freezing

  • Thread starter Thread starter John2000k
  • Start date Start date
J

John2000k

Guest
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
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 also include the code for the "Start scan" button which the user will press to begin process:


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!

Continue reading...
 
Back
Top