Textbox doesn't get updated while threading

后端 未结 3 638
囚心锁ツ
囚心锁ツ 2021-01-24 07:52

Here is a sub code inside a module named \"Blahing\":

    Sub BlahBlah(ByVal Count As Long)
        For i As Long = 0 To Count
            frmBlaher.txtBlah.Appe         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-24 08:33

    This is because you are trying to update a control from a thread other than the one which created it. You can get past this with the Control.Invoke and Control.InvokeRequired methods. Control.Invoke will run the passed in delegate on the thread which created the Control.

    I don't work with VB at all but you could try something along the lines of this:

    Delegate Sub BlahBlahDelegate(ByVal Count As Long)
    
    Sub BlahBlah(ByVal Count As Long)
        If frmBlaher.txtBlah.InvokeRequired Then
            Dim Del As BlahBlahDelegate
            Del = new BlahBlahDelegate(AddressOf BlahBlah)
            frmBlaher.txtBlah.Invoke(Del, New Object() { Count })
        Else
            For i As Long = 0 To Count
                frmBlaher.txtBlah.AppendText("Blah")
            Next
        End If
    End Sub
    

提交回复
热议问题