vb.net - 后台线程问题
由于某种原因,我的应用程序中的后台线程无法更改主窗体上的任何标签、文本框值等。没有编译错误,当线程执行时什么也没有发生。
这是一些示例代码:
Imports System.Threading
Public Class Class1
Dim tmpThread As System.Threading.Thread
Private Sub bgFindThread()
Form1.lblStatus.Text = "test"
End Sub
Public Sub ThreadAction(ByVal Action As String)
If Action = "Start" Then
tmpThread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf bgFindThread))
tmpThread.Start()
ElseIf Action = "Abort" Then
If tmpThread.IsAlive = True Then tmpThread.Abort()
End If
End Sub
End Class
有人可以让我知道我做错了什么吗?
For some reason a background thread in my app can't change any labels, textbox values, etc on my main form. There is no compile errors, when the thread executes nothing happens.
Here is some example code:
Imports System.Threading
Public Class Class1
Dim tmpThread As System.Threading.Thread
Private Sub bgFindThread()
Form1.lblStatus.Text = "test"
End Sub
Public Sub ThreadAction(ByVal Action As String)
If Action = "Start" Then
tmpThread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf bgFindThread))
tmpThread.Start()
ElseIf Action = "Abort" Then
If tmpThread.IsAlive = True Then tmpThread.Abort()
End If
End Sub
End Class
Can someone let me know what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AFAIK 上面的代码会抛出异常 IllegalCrossThreadException,这是因为后台线程与 UI 线程不同,并且后台尝试在其他线程上设置值。所以Windows窗体检查每个正常工作的线程。
您可以将 Control.CheckForIllegalCrossThreadCalls 设置为 false 以使其正常工作。
下面的代码是当设置属性未运行时
该方法异步地将代码从后台线程运行到UI线程。
AFAIK code above will throw an exception IllegalCrossThreadException, it is because the background thread is not the same as UI thread and background try to set value on other thread. So windows form check every thread that work properly.
You can set Control.CheckForIllegalCrossThreadCalls to false to make it works.
Code below is when setting property is not run
The method asyncronsly run code from background thread to UI thread.
您只能从 UI 线程访问 UI 控件。
我建议先阅读此内容: http://www.albahari.com/threading/
You can only access UI controls from the UI thread.
I suggest reading this first: http://www.albahari.com/threading/
正如其他人提到的,禁止(出于充分的理由)从非 UI 线程更新 UI 元素。
规范的解决方案如下:
在您的情况下:
唯一改变的是方法开头的保护子句,它测试我们是否在 UI 线程内,如果不在,则请求在 UI 线程中执行并返回。
As others have mentioned, it is forbidden (for good reasons) to update UI elements from a non-UI thread.
The canonical solution is as follows:
In your case:
The only thing that changed is the guard clause at the beginning of the method which test whether we’re inside the UI thread and, if not, requests an execution in the UI thread and returns.
您可以使用委托来更新后台线程中的 UI 控件。
例子
You can use a delegate to update UI controls in a background thread.
Example