Need Help with multi-threading.

FunnyX

New member
Joined
Apr 7, 2009
Messages
4
Programming Experience
10+
I'm making a application which will search a text file for a specific string, each line has 1 word, no spaces. I can't seem to get it to display anything while its searching, other then the progress in the progress bar, and if you minimize it while its searching and bring it back up it will only refresh the progress bar.

Can anyone help me?

Here is the code:
VB.NET:
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

        If PswFile = Nothing Then Exit Sub
        Password = txtPassword.Text

        Dim FileContents() As String = IO.File.ReadAllLines(PswFile)

        lblUsrBorder.Focus()
        pgbrProgress.Value = 0
        pgbrProgress.Maximum = FileContents.Length

        For I = 0 To FileContents.Length - 1
            pgbrProgress.Value += 1
            tsslStatus.Text = "Processing Password: " & Format(pgbrProgress.Value / pgbrProgress.Maximum, "#0%")
            Dim LineParts() As String = FileContents(I).Split("'")
            If LineParts(0) = Password Then
                tsslStatus.Text = "PASSWORD FOUND! #" & I & "(of " & FileContents.Length & ")"
                fwFlash.FlashWindow(Me, FlashWindow.enuFlashOptions.FLASHW_ALL, 9999)
                Exit Sub
            End If
        Next

        fwFlash.FlashWindow(Me, FlashWindow.enuFlashOptions.FLASHW_ALL)
End Sub

Thanks in advance for the help.
 
Because you're doing everything on the UI thread, it is too busy doing your work to refresh the UI. You need to do your work on a secondary thread and only use the UI thread for updating the UI. Your simplest option is to use a BackgroundWorker. Check here for examples:

Using the BackgroundWorker Component
 
Back
Top