multithreading - 线程化时文本框未更新

标签 multithreading textbox

这是名为“Blahing”的模块内的子代码:

    Sub BlahBlah(ByVal Count As Long)
        For i As Long = 0 To Count
            frmBlaher.txtBlah.Appendtext("Blah")
        Next
    End Sub

这是名为 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

当我在 txtBlahCount 中输入任何数字(例如 10)然后按下 WriteBlah 按钮时,什么也没有发生。我设置了多个断点,我发现“Appendtext”部分出现但不起作用。我检查了 txtBlah 的 Text_Changed 事件,它发生了,但唯一的问题是,我在 txtBlah 中看不到任何文本。我是多线程的新手。我之前读过很多关于这个问题的答案,但没有一个给出一个例子。你能帮忙吗?

最佳答案

运行你的代码有点不同,这就是 vb.net 中多线程的结构应该是什么样的(它与 Vb.net 没有将命名空间传递给我理解的模型有关)

这将是您从 MainThread 加载的 startThread 或 w/e 有您

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

这将是实际线程本身(新模型/类或同一类)
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

为每次调用主线程编写一个委托(delegate)和调用方法。
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

这是在 vb.net 中编写线程的测试(非挂起)方式

如果您需要进一步帮助将您的代码实现到此模板,请告诉我:P

关于multithreading - 线程化时文本框未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17867358/

相关文章:

c++ - 在调用仿函数时删除它是否安全?

c# - TextBox 不遵守 Get 返回的值

vb.net - 强制 MultiLine TextBox 水平滚动条向左

c# - 如何在文本框中获取选定的行?

c++ - promise 可以使用结构类型吗?

c++ - Android JNI——线程同步

java - 线程不会停止使用标志作为 volatile

java - 诊断 Java 中断标志

Javascript 启用已禁用的文本框

uwp - UWP 中的 TextBox 未触发 KeyDown 事件