Textbox doesn't get updated while threading

后端 未结 3 636
囚心锁ツ
囚心锁ツ 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:12

    Run your code a little bit different, This is how the Structure should look like for Multithreading in vb.net ( it has something to do with Vb.net not passing Namespaces into Models from what i Understand )

    This would be your startThread from MainThread in load or w/e have you

    Private Sub DoSomethingSimple()
        Dim DoSomethingSimple_Thread As New Thread(AddressOf DoSimple)
        DoSomethingSimple_Thread.Priority = ThreadPriority.AboveNormal
        DoSomethingSimple_Thread.Start(Me)
    End Sub
    

    This would be the actual thread Itself ( new model / class or in the same class )

    Private Sub DoSimple(beginform As Form)
        'Do whatever you are doing that has nothing to do with ui
    
        'For UI calls use the following
        SomethingInvoked(PassibleVariable, beginform)
    
    End Sub
    

    Write a Delegate and Invoke Method for Each Call to the Main Thread.

    Delegate Sub SomethingInvoked_Delegate(s As Integer, beginform As Form)
    Sub SomethingInvoked_Invoke(ByVal s As Integer, beginform As Form)
        If beginform.NameOfControlYouAreUpdating.InvokeRequired Then ' change NameOfControlYouAreUpdating to the Name of Control on the form you wish to update
            Dim d As New SomethingInvoked_Delegate(AddressOf SomethingInvoked_Invoke)
            beginform.Invoke(d, New Object() {s, beginform})
        Else
    
            'Do something...
            beginform.NameOfControlYouAreUpdating.Condition = Parameter
    
        End If
    End Sub
    

    This is tested ( non hanging ) way of writing Threads in vb.net

    If you need further help implementing your code to this Template let me know :P

    0 讨论(0)
  • 2021-01-24 08:22

    Take a look at the MSDN site, which will give you everything you need. You especially need to take notice of the SetText method and its use of the InvokeRequired and Invoke method, as well as its use of delegates.

    It may seem daunting at first, but once you get the hang of it, it becomes second nature.

    Here's a link http://msdn.microsoft.com/en-us/library/ms171728(v=vs.80).aspx

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题