控制新线程的按钮

时间:2016-06-25 17:36:08

标签: vb.net multithreading

我正在尝试从单独的线程更改表单上多个按钮的背景颜色。我认为我遇到的问题与我使用字典访问按钮有关。这是我的代码:

Public Shared numsToDisplay As New List(Of Integer)
Public Shared buttons As New Dictionary(Of String, Control)


Private disThread As Thread

Public Sub DisplayNums()
    disThread = New Thread(AddressOf Display)
    disThread.IsBackground = True
    disThread.Start()
End Sub

Private Sub Display()
    buttons.Add("Button1", Button1)
    buttons.Add("Button2", Button2)
    buttons.Add("Button3", Button3)
    buttons.Add("Button4", Button4)
    buttons.Add("Button5", Button5)
    buttons.Add("Button6", Button6)
    buttons.Add("Button7", Button7)
    buttons.Add("Button8", Button8)
    buttons.Add("Button9", Button9)
    buttons.Add("Button10", Button10)
    buttons.Add("Button11", Button11)
    buttons.Add("Button12", Button12)
    buttons.Add("Button13", Button13)
    buttons.Add("Button14", Button14)
    buttons.Add("Button15", Button15)
    buttons.Add("Button16", Button16)
    buttons.Add("Button17", Button17)
    buttons.Add("Button18", Button18)
    buttons.Add("Button19", Button19)
    buttons.Add("Button20", Button20)
    buttons.Add("Button21", Button21)
    buttons.Add("Button22", Button22)
    buttons.Add("Button23", Button23)
    buttons.Add("Button24", Button24)
    buttons.Add("Button25", Button25)
    buttons.Add("Button26", Button26)
    buttons.Add("Button27", Button27)
    buttons.Add("Button28", Button28)
    buttons.Add("Button29", Button29)
    buttons.Add("Button30", Button30)
    buttons.Add("Button31", Button31)
    buttons.Add("Button32", Button32)
    buttons.Add("Button33", Button33)
    buttons.Add("Button34", Button34)
    buttons.Add("Button35", Button35)
    buttons.Add("Button36", Button36)
    buttons.Add("Button37", Button37)
    buttons.Add("Button38", Button38)
    buttons.Add("Button39", Button39)
    buttons.Add("Button40", Button40)
    buttons.Add("Button41", Button41)
    buttons.Add("Button42", Button42)
    buttons.Add("Button43", Button43)
    buttons.Add("Button44", Button44)
    buttons.Add("Button45", Button45)
    buttons.Add("Button46", Button46)
    buttons.Add("Button47", Button47)
    buttons.Add("Button48", Button48)
    buttons.Add("Button49", Button49)
    buttons.Add("Button50", Button50)
    buttons.Add("Button51", Button51)
    buttons.Add("Button52", Button52)
    buttons.Add("Button53", Button53)
    buttons.Add("Button54", Button54)
    buttons.Add("Button55", Button55)
    buttons.Add("Button56", Button56)
    buttons.Add("Button57", Button57)
    buttons.Add("Button58", Button58)
    buttons.Add("Button59", Button59)
    buttons.Add("Button60", Button60)
    buttons.Add("Button61", Button61)
    buttons.Add("Button62", Button62)
    buttons.Add("Button63", Button63)
    buttons.Add("Button64", Button64)
    buttons.Add("Button65", Button65)
    buttons.Add("Button66", Button66)
    buttons.Add("Button67", Button67)
    buttons.Add("Button68", Button68)
    buttons.Add("Button69", Button69)
    buttons.Add("Button70", Button70)
    buttons.Add("Button71", Button71)
    buttons.Add("Button72", Button72)
    buttons.Add("Button73", Button73)
    buttons.Add("Button74", Button74)
    buttons.Add("Button75", Button75)
    buttons.Add("Button76", Button76)
    buttons.Add("Button77", Button77)
    buttons.Add("Button78", Button78)
    buttons.Add("Button79", Button79)
    buttons.Add("Button80", Button80)
    For Each num In numsToDisplay
        SetColor(num)
    Next
End Sub

Delegate Sub SetColorCallback([num] As Integer)

Private Sub SetColor(ByVal [num] As Integer)

    ' InvokeRequired required compares the thread ID of the
    ' calling thread to the thread ID of the creating thread.
    ' If these threads are different, it returns true.
    If buttons("Button" & [num]).InvokeRequired Then
        Debug.Print("Invoking")
        Dim d As New SetColorCallback(AddressOf SetColor)
        Me.Invoke(d, New Object() {[num]})
    Else
        buttons("Button" & [num]).BackColor = Color.Red
    End If
End Sub

DisplayNums()由单独的表单调用。即使应该调用也不会发生调用。我也没有因为尝试通过错误的线程访问表单而导致任何错误。

0 个答案:

没有答案