线程化时文本框不会更新 [英] Textbox doesn't get updated while threading

查看:48
本文介绍了线程化时文本框不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个名为Blahin"的模块中的子代码:

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

这是一个名为 frmBlaher 的表单中的按钮单击事件代码:

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

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

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?

推荐答案

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

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 )

这将是您加载的 MainThread 中的 startThread,或者您有您的情况

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 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 中测试(非挂起)编写线程的方式

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

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

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

这篇关于线程化时文本框不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆