0

Hello everyone I'm currently making an antivirus thing for fun in vb.net and when my antivirus is doing the scan the program is slow and lagging can i make it faster? here's the part i think needs changing. UPDATE: that is the code that is in my timer can anyone help?

    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
            Label1.Text = ListBox1.SelectedItem.ToString
        Catch ex As Exception
        End Try



        Try

            Dim scanbox As New TextBox
            Dim read As String = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Database\VirusList.dat")
            ProgressBar1.Increment(1)
            detected.Text = Conversions.ToString(Quarantine.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



                Quarantine.ListBox2.Items.Add(ListBox1.SelectedItem)


            End If
        Catch ex As Exception
        End Try
    Else
        Timer1.Stop()

        MsgBox("Finished Scanning")
        Quarantine.Label3.Text = "Pending Quarantine"
        Quarantine.Label2.Text = "Select Item then right click and choose quarantine or delete"
        Quarantine.Label3.ForeColor = Color.DarkRed
        Quarantine.Label2.Location = New Point(142, 518)
        Quarantine.Label2.ForeColor = Color.DarkRed
        Quarantine.QuarantineToolStripMenuItem.Visible = True
        Quarantine.Show()
        RealTime.CheckBox2.Checked = True

        ProgressBar1.Value = 0
        If ListBox1.Items.Count = 0 Then
            MsgBox("No Threats were detected", MsgBoxStyle.Information)
            WriteToLog("No Threats were detected")

        End If
    End If
  • 1
    This really isn't much to go on... Lagging _how_? How slow is _"slow"_? Instead of providing the code _you think_ needs changing, debug your code and determine _where_ the bottleneck is, then share a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Visual Vincent Sep 16 '17 at 12:31
  • im not following dude i mean the program goes very slow if i hover over and button while the scan in running it take a few seconds to register and its not good to have it slow. any ideas when i debug the program its all fine and when he scans done its fine just while its scanning its slow. – Brandon Williams Sep 16 '17 at 12:38
  • 1
    My point is that the code in your question is _far_ from enough for us to be able to identify any issues. It is up to you to step through or measure the execution time of your code and identify which part is going so slow. – Visual Vincent Sep 16 '17 at 13:47
  • Though based on your comment it sounds like you are doing too much processing on the UI thread, which causes it to become slow/unresponsive. I take it that you do not use multithreading? Here's a small guide for that: https://stackoverflow.com/a/45571728/3740093 (start at _**The basics**_) – Visual Vincent Sep 16 '17 at 13:48
  • cheers dude ill check that out now – Brandon Williams Sep 16 '17 at 15:23
  • @VisualVincent dude this is the code ecause i didnt understand the link here: – Brandon Williams Sep 16 '17 at 15:31
  • dam its too long – Brandon Williams Sep 16 '17 at 15:31
  • ill edit my awnser – Brandon Williams Sep 16 '17 at 15:31
  • What part(s) didn't you understand? I'll clarify it/them for you. -- The main principle is: Create a thread when you want to do some heavy work (i.e. iterate through a large amount of files and do processing on each), then **every time** you want to access the UI (_User Interface_, meaning anything the user can see: labels, progress bars, text boxes, buttons, and so on...) you must marshal that call via `Control.Invoke()`. It's all described in my answer, with example snippets. In the bottom you'll also find a complete code example. – Visual Vincent Sep 16 '17 at 16:20
  • oh ok cheers dude – Brandon Williams Sep 16 '17 at 17:11

0 Answers0