从 VB.NET 中的另一个类/线程访问 TextBox 控件时出现问题
我试图从另一个类和线程访问 Richtextbox,然后 ui 和我似乎无法让它工作。我目前有一个处理公共类内部工作的子程序,它是一个共享子程序,我将从几个不同的类和线程中调用它,但我似乎无法让它工作。代码如下所示。
Public Class SharedSubs
Public Shared Sub console(ByVal message As String)
Dim c As New Form1
If c.consoleBox.Text.Length > 0 Then
If c.consoleBox.Text.Substring(c.consoleBox.Text.Length - 3, 3) = "..." Then
c.consoleBox.AppendText(message)
ElseIf c.consoleBox.Text.Substring(c.consoleBox.Text.Length - 1, 1) = "." Then
c.consoleBox.AppendText(ControlChars.NewLine & timeStamp() & message)
End If
Else
c.consoleBox.AppendText(timeStamp() & message)
End If
End Sub
我将使用 SharedSubs.Console("stringstring")
调用 sub,当从 ui 线程调用它时,它工作正常,但是当从任何其他线程或类调用它时,它什么也不做,我确认它会通过这段代码,但它没有在文本框中显示任何内容,并且由于我认为会出现委托,它也没有通过异常。
我这样做完全错误吗?一些帮助会很好,谢谢。
im trying to access a richtextbox from another class and thread then the ui and i cant seem to get it working. I currently have the sub thats handling the the work inside of a public class and its a sharedsub, im going to be calling it from several different classes and threads but again i cant seem to get it working. the code looks like this.
Public Class SharedSubs
Public Shared Sub console(ByVal message As String)
Dim c As New Form1
If c.consoleBox.Text.Length > 0 Then
If c.consoleBox.Text.Substring(c.consoleBox.Text.Length - 3, 3) = "..." Then
c.consoleBox.AppendText(message)
ElseIf c.consoleBox.Text.Substring(c.consoleBox.Text.Length - 1, 1) = "." Then
c.consoleBox.AppendText(ControlChars.NewLine & timeStamp() & message)
End If
Else
c.consoleBox.AppendText(timeStamp() & message)
End If
End Sub
i would be calling sub using SharedSubs.Console("stringstring")
when this is called from the ui thread it works fine but when its called from anyother thread or class its does nothing, ive confirmed it going thru this code but its not displaying anything in the textbox and its not thru an exception due to delegates either which i figured it would.
Am i doing this completely wrong? some help would be great, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法从主/UI 线程以外的线程修改 Winforms UI 组件的显示。要从另一个线程更新,请查看 Control.Invoke 的文档。它有一个很好的例子。
http://msdn.microsoft.com/en -us/library/zyzhdc6b%28v=VS.100%29.aspx
You can't modify the display of a Winforms UI component from a thread other than the main/UI thread. To update from another thread, check out Control.Invoke's documentation. It has a nice example.
http://msdn.microsoft.com/en-us/library/zyzhdc6b%28v=VS.100%29.aspx
理想情况下,使用一个事件,然后您可以将其他内容附加到您的 Textupdate...例如您可能想要显示它,并记录它...然后您只需将表单和日志绑定到事件。
如果您确实想在表单上调用该方法,那么您需要在正确的线程上调用它。这可以通过两种方式完成。
在创建线程的类中,将对表单/控件的引用传递到构造函数中(因此此时位于同一线程上)。将参考存储在您的班级中。然后创建您的线程。 (不要使用共享,以便引用正确的实例)
在您的线程中,您只需调用表单即可。
或者
从您的线程中调用表单上的方法以及表单上的方法内部。
Ideally, use an event, then you can have other things attached to your Textupdate... Eg You might want to display it, and log it ... then you just bind the form and the log to the event.
If you really want to call the method on your form, then you'll need to call it on the proper thread. This can be done 2 ways.
In the class where you are creating the Thread you pass a reference to the Form/Control into the constructor (and therefore that is on the same thread at that point). Store the reference in your class. Then create your Thread. (Don't use the shared so that you are referring to the correct instance)
In your Thread, you then just Invoke the Form.
OR
From your thread you Call the method on your form, and inside the Method on the form.
尝试在表单加载上添加此代码:
Me.CheckForIllegalCrossThreadCalls = False
它应该可以解决问题
Try adding this code on form load:
Me.CheckForIllegalCrossThreadCalls = False
It should fix the problem