gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:20:30 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com