这是名为“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 不将命名空间传递到模型中有关,据我所知)
这将是您在加载中从主线程开始的线程,或者您有
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 中编写线程的测试(非挂起)方法
如果您需要进一步帮助将代码实现到此模板,请告诉我:P
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)