[2008] BackgroundWorker Problem
I am trying to check URLs for naughty words by using the following
Quote:
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
Public Class Form1
Inherits System.Windows.Forms.Form
Private WithEvents bgw1 As System.ComponentModel.BackgroundWorker
Dim Reader As New System.IO.StreamReader (Application.StartupPath & "\blacklist.txt")
Dim textline As String
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.bgw1.RunWorkerAsync()
End Sub
Public Sub New()
InitializeComponent()
End Sub
Private Sub bgw1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw1.DoWork
Dim bgw1 As BackgroundWorker = CType(sender, BackgroundWorker)
textline = Reader.ReadLine
Do Until Reader.EndOfStream
If ComboBox1.Text.Contains(textline) Then
bgw1.CancelAsync()
Exit Sub
Else
WebBrowser1.Navigate(ComboBox1.Text)
End If
Loop
End Sub
Private Sub bgw1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw1.RunWorkerCompleted
Reader.Close()
If e.Cancelled Then
WebBrowser1.Navigate("about:blank")
End If
Me.bgw1.RunWorkerAsync()
End Sub
End Class
It is supposed to constantly check the text in the address bar for any words on the "blacklist.txt" without affecting the rest of the form. But it doesn't seem to want to work!
What am I doing wrong?
Re: [2008] BackgroundWorker Problem
First, I don't think doing this with a background worker is corrent. I think you'd better hang this to the combobox1.textchanged event...
Second, I'd make a collection or list for the blacklisted word. This way you can easely add/remove/find blacklisted words
Thrid, you are 'reading' the text file to often, read it to memory once, at startup