vb2012 textbox backcolor不会改变使用线程

时间:2014-06-30 03:54:52

标签: vb.net multithreading textbox

我写了非常简单的线程示例。

  1. 制作普通form1并删除1个文本框
  2. 在表单加载上运行线程工作
  3. 线程更改文本框背景颜色看起来像开/关
  4. 但是,它不起作用.....

    你能告诉我为什么不工作吗?

    看我的来源。

    =============================================== ======================

    Imports System.Threading
    
    Public Class Monitor
        Public wObj As Worker = New Worker()
        Public MyThread As Thread = New Thread(AddressOf wObj.DoWork)
    
        Public Sub ChangeTBColor(pOption As Integer)
            If pOption = 1 Then
                tb1.BackColor = Color.Aqua
            Else
                tb1.BackColor = Color.Red
            End If
        End Sub
    
    
    
        Private Sub Monitor_Load(sender As Object, e As EventArgs) Handles Me.Load
    
            MyThread.Start()
    
            Console.WriteLine("Running OrgThread..")
    
            Console.WriteLine("Stop running")
    
        End Sub
    
        Private Sub BtnThreadStop_Click(sender As Object, e As EventArgs) Handles BtnThreadStop.Click
    
            Me.wObj.RequestStop()
        End Sub
    
    End Class
    
    
    Public Class Worker
        Private LoopStop As Boolean = True
        Public Sub DoWork()
            Console.WriteLine("User Thread Start!")
            Dim iTemp As Integer = 0
    
            While (LoopStop)
    
                Monitor.ChangeTBColor(iTemp Mod 2)
    
                iTemp = iTemp + 1
    
                Thread.Sleep(500)
    
            End While
    
            Console.WriteLine("User Thread End.!")
        End Sub
    
        Public Sub RequestStop()
            LoopStop = False
        End Sub
    End Class
    

1 个答案:

答案 0 :(得分:0)

如前所述,您为工作开始一个新线程,问题是您尝试更改需要调用的控件的颜色。有了这个说,我们需要一个委托来控制需要调用...在我的例子中,我使用了一个处理这一切的类并且工作得很好。另外请确保导入System.ComponentModel,因为BackgroundWorker需要这样做...我使用后台工作人员,因为它需要你需要的所有繁重的工作......

Imports System.ComponentModel
Imports System.Threading

Public Class Monitor
Delegate Sub SetColor(ByVal clr As Integer) 'Your delegate..
Private WithEvents bw As New BackgroundWorker

Public Sub ChangeTBColor(pOption As Integer)
    If Me.tb1.InvokeRequired Then 'Invoke if required...
        Dim d As New SetColor(AddressOf ChangeTBColor) 'Your delegate...
        Me.Invoke(d, New Object() {pOption})
    Else
        If pOption = 1 Then
            tb1.BackColor = Color.Aqua
        Else
            tb1.BackColor = Color.Red
        End If
    End If


End Sub

Private Sub Monitor_Load(sender As Object, e As EventArgs) Handles Me.Load
    bw.WorkerSupportsCancellation = True
    Console.WriteLine("Running OrgThread..")
    bw.RunWorkerAsync()

End Sub

Private Sub bw_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bw.DoWork
    Dim iTemp As Integer = 0
    Dim LoopStop As Boolean = True
    Console.WriteLine("User Thread Start!")

    While (LoopStop)
        If Not (bw.CancellationPending) Then
            ChangeTBColor(iTemp Mod 2)
            iTemp = iTemp + 1
            Thread.Sleep(500)
        Else
            e.Cancel = True
            LoopStop = False
        End If
    End While

End Sub

Private Sub bw_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted
    Console.WriteLine("User Thread End.!")
End Sub

Private Sub BtnThreadStop_Click(sender As Object, e As EventArgs) Handles BtnThreadStop.Click
    If bw.IsBusy Then
        bw.CancelAsync()
    Else
        Console.WriteLine("Running OrgThread..")
        bw.RunWorkerAsync()
    End If
End Sub

End Class

这是我的测试截图...这是经过试用和测试的。如果这对您有帮助,请务必投票!

enter image description here