I'm having problems with the following code:
The event handler is called after each turn in the loop ( 0 to 10 )
The results are output to a RichTextBox, but with only 11 iterations
happening, I sometimes get duplicate results
My final output should only have 11 groups of keywords, but sometimes, group
9 or group 10 will have been inserted into the Rich text box 2 or 3
additional times.
Can anyone tell me by reading this code if they can spot what is happening?
From Form1.vb:
----------------------
Dim objKeywords As New Keywords
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim strTemp As String = Keywords.Wizard(TextBox1.Text)
Dim strArray() As String = Split(strTemp, "|")
Dim kwCount As Integer = UBound(strArray) - 1
AddHandler objKeywords.DoneCounting, AddressOf
DoneCountingEventHandler
For i As Integer = 0 To 10
ThreadArray(i) = New System.Threading.Thread(AddressOf
objKeywords.kwLoop
objKeywords.kwRootKeyword = strArray(i)
objKeywords.kwMaxCount = 10
objKeywords.Groups = i
ThreadArray(i).Name = "Thread " & i
ThreadArray(i).Start()
Next
End Sub
Sub DoneCountingEventHandler(ByVal strTemp1 As String)
Dim strTemp2 = Split(strTemp1, "|")
Dim intCount As Integer
For intCount = 0 To UBound(strTemp2) - 1
RichTextBox1.AppendText(strTemp2(intCount) & ControlChars.CrLf)
Next
End Sub
------
From Class1.vb in same project
------
Imports System
Imports System.IO
Imports System.Net
Public Class Keywords
Public kwRootKeyword As String
Public kwMaxCount As Integer
Public Event DoneCounting(ByVal strArray As String)
Public Groups As Integer
Function Wizard(ByVal RootKeyword As String)
...
End Function
Public Sub kwLoop()
Dim strArrayTest As String = "--- Group " & Groups &
ControlChars.CrLf & Wizard(kwRootKeyword)
RaiseEvent DoneCounting(strArrayTest)
strArrayTest = Nothing
End Sub
End Class