有很多问题与此问题重复,但似乎都没有真正回答我的具体问题。
我正在编写一个 IRC 机器人,并且我正在将用户添加到 List(Of String),如您在下面的代码中所见。但是,当我想通过另一个线程上的用户的 foreach 循环时,我得到标题中的错误。我什至设置了一个布尔值来阻止它添加用户,但它仍然说它正在以某种方式被修改。任何帮助将不胜感激。
(在阅读代码之前,请记住我是 VB.net 的新手,即使它很乱,我也不想因为我是新手而受到抨击)
Sub Handler()
handlerActive = True
Dim list As New List(Of String)
query = "SELECT * FROM " & channel.Substring(1)
Try
connHost.Close()
Catch
End Try
connHost.Open()
Dim cmd As MySqlCommand = New MySqlCommand(query, connHost)
Dim dataReader As MySqlDataReader = cmd.ExecuteReader()
While (dataReader.Read())
list.Add(dataReader.GetString(1))
End While
dataReader.Close()
If (users.Count > 0) Then
For Each user2 In users
Try
connHost.Close()
Catch ex As Exception
End Try
connHost.Open()
query = ("UPDATE `" & channel.Substring(1) & "` SET `Amount` = Amount + 1 WHERE `Username`='" & user2 & "'")
cmd = New MySqlCommand(query, connHost)
Try
cmd.ExecuteNonQuery()
Catch
Console.WriteLine("Failed to update user - " & user2)
End Try
Next
End If
connHost.Close()
handlerActive = False
End Sub
下面是我将用户添加到名为“用户”的列表的位置
If (handlerActive = False And users.Contains(user) = False And user.Trim().CompareTo("") <> 0 Or user.Trim().CompareTo("jtv") <> 0 Or user.Trim().Contains(" ") = False Or user.Trim().Contains(".") = False) Then
users.Add(user)
End If