Hello all, I have been having some trouble ever since I implemented this background worker code.

My program simply loads a script (simply a file with one line containing a REGEX, although the script can contain more then one line of REGEX) and runs the check against everyline of every log file. When I run the script (by clicking Run) it seems to work perfectly, but if I either Press Stop, OR, wait for it to finish, the whole program freezes up for about 5 seconds, and then what I see in the first screenshot, turns into the second screenshot... Any help?





This is just a snippet of the code, but I can post the whole lot upon request. Thanks
Code:
Imports System
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.ComponentModel

Public Class MainWindow

    Public num_logs As Integer
    Public num_scripts As Integer
    Public script_error As Integer
    Public script_edisc As String
    Public script_file As String
    Public script_letter As String
    Public script_loop As Decimal
    Public log_file As String
    Public log_letter As String
    Public log_loop As Decimal
    Public sr As StreamReader
    Public lr As StreamReader
    Public read_file_line As String
    Public current_script As String
    Public running_script As String
    Public script_line As String
    Public log_line As String
    Public script_param As Array
    Public script_text As String
    Public script_docu As String
    Public response As Boolean
    Public editmode As Integer
    Public script_regex As Regex
    Public script_m As Match
    Public running_bg As Integer

    Private Sub ToolStripButton5_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton5.Click
        If (ListBox2.Items.Count < 1) Then
            MsgBox("There are no items in the script list. There needs to be at least one script selected before you can run it.", MsgBoxStyle.OkOnly, "Run Script")
        Else
            If (CheckedListBox1.Items.Count < 1) Then
                MsgBox("The logs list is empty. There are no logs to run this script against.", MsgBoxStyle.OkOnly, "Run Script")
            Else
                If Not BackgroundWorker1.IsBusy = True Then
                    BackgroundWorker1.RunWorkerAsync()
                    running_bg = 1
                End If


            End If
        End If


    End Sub


    Private Sub MainWindow_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False

~ More Code ~

End Sub

    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim worker As System.ComponentModel.BackgroundWorker
        worker = CType(sender, System.ComponentModel.BackgroundWorker)

        current_script = ListBox2.GetItemText(ListBox2.SelectedItem)
        TabControl1.SelectTab(1)
        If current_script <> "" Then
            running_script = "Running '" + current_script + "' script on selected log files..."
            al(running_script)
            script_error = 0
            script_edisc = ""

            For Each logfile In CheckedListBox1.CheckedItems()

                log_file = ToolStripTextBox2.Text + "\" + logfile.ToString + ".log"
                ListBox3.Items.Add("Running script on " + log_file + "...")
                Me.Refresh()
                lr = New StreamReader(log_file)
                Do
                    log_line = lr.ReadLine()
                    If log_line <> "" Then


                        sr = New StreamReader("scripts/" + current_script + ".lcs")
                        Do
                            script_line = sr.ReadLine()
                            If (script_line <> "") Then
                                If running_bg = 0 Then

                                    Exit Do
                                End If
                                If script_line.Length > 6 Then
                                    If (script_line.Substring(0, 6) = "MATCH ") Then
                                        Dim script_regex As New Regex(script_line.Substring(6))
                                        Dim script_m As Match = script_regex.Match(log_line)
                                        If script_m.Success Then
                                            ListBox3.Items.Add(log_line)
                                            Me.Refresh()
                                        End If
                                    End If
                                End If

                            End If

                        Loop Until script_line Is Nothing
                    End If
                    If running_bg = 0 Then
                        Exit Do
                    End If
                Loop Until log_line Is Nothing
                ListBox3.Items.Add("Finished running script on " + log_file + "...")
                Me.Refresh()
                If running_bg = 0 Then
                    Exit For
                End If
            Next

            al("Script has finished running.")
            Me.Refresh()
        End If
    End Sub

    Private Sub ToolStripButton11_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton11.Click
        If BackgroundWorker1.WorkerSupportsCancellation = True Then
            BackgroundWorker1.CancelAsync()
            running_bg = 0
        End If
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted   
        If e.Error IsNot Nothing Then
            al("Error: " & e.Error.Message)
        ElseIf e.Cancelled Then
            al("Script did not finish as it was stopped.")
        Else
            al("Done!")
        End If
    End Sub


End Class