Textbox doesn't get updated while threading

扶醉桌前 提交于 2019-12-20 05:31:37

问题


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.Appendtext("Blah")
        Next
    End Sub

Here is a button click event code inside a form called frmBlaher:

     Private Sub WriteBlah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WriteBlah.Click
         Dim Thread As New Threading.Thread(Sub() Blahing.BlahBlah(Val(_
              TxtBlahCount.Text)))

         Thread.Start()
     End Sub

When I type any number in txtBlahCount (for example 10) and then press the WriteBlah button, nothing happens. I set multiple breakpoints, and I found that the "Appendtext" part occurs but does not work. I checked the Text_Changed event of the txtBlah and it occur, but the only problem, I don't see any text in txtBlah. I'm new to multithreding. and I read many answers to this question before, but none of them showed an example. Could you help?


回答1:


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




回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/17867358/textbox-doesnt-get-updated-while-threading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!