收集被修改;枚举操作可能无法执行。 VB.Net

时间:2013-08-22 03:33:04

标签: vb.net bots irc

有很多问题与这个问题重复,但它们似乎都没有真正回答我的具体问题。

我正在编写一个IRC bot,我正在将用户添加到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

下面是我将用户添加到名为“users”

的列表中的位置
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

0 个答案:

没有答案