从类跨线程调用?困惑-vb.net [英] Cross Thread invoke from class ? Confused - vb.net

查看:226
本文介绍了从类跨线程调用?困惑-vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我被弯腰了...但是事实是我有点担心线程问题...

我正在上课使用串行端口.我正在将一个事件从该类引发到我的窗体中,称为该类.事件包含收到的数据...

我只想从引发的事件中填充一个文本框.

现在我没有专门创建单独的线程,但是在尝试更新UI上的文本框时遇到了正常的跨线程错误,因此我的假设是串行端口及其内部方法可能会创建自己的线程...

无论如何,我对于如何正确地从我的主要形式实现指向实例化类中的线程的调用感到有些困惑...

我希望这是有道理的...

Dim WithEvents tmpRS232 As New clsRS232

有人可以提供基于此代码的代码示例吗?

谢谢.

解决方案

您是正确的,这里的问题是您正在尝试从非UI线程(在本例中为串行端口处理程序)更新UI元素.您需要做的是检查您尝试从回调访问的控件上是否设置了InvokeRequired标志.如果是这样,则意味着您需要整理对UI线程的调用.您可以通过使用 System.Windows.Forms.Control 中的Invoke或BeginInvoke来实现. /p>

Private Delegate Sub SetRxTextCallback(ByVal [text] As String)

Private Sub SetRxText(ByVal [text] As String)
    txt_rx.Text = [text]
End Sub

Private Sub tmprs232_rx_data_returned(ByVal str_data As String) Handles tmpRS232.rx_data_returned

    If (txt_rx.InvokeRequired) Then
        Dim d As New SetRxTextCallback(AddressOf Me.SetRxText)
        Me.BeginInvoke(d, New Object() {[str_data]})
    End If

    'txt_rx.Text = str_data 'Cross threading error
    'MsgBox(str_data) 'Fires without errors
End Sub

这里是 MSDN 文档的链接,该文档说明了详细.

maybe I am being stooped... but the fact is that I am a bit of a n00b concerning threading...

I am making use of a serial port in a class. I am raising an event from that class to my form calling the class. Event contains data received...

I wish to simply populate a textbox from the raised event.

Now I am not specifically creating a seperate thread, but I get the normal crossthreading error when trying to update my textbox on the UI, so my assumption is that the serial port and its internal methods probably creates its own threads...

Regardless, I am a bit confused as to how to properly implement an invoke, from my main form, pointing to the thread in the instantiated class...

I hope this makes sense...

Dim WithEvents tmpRS232 As New clsRS232

Private Sub but_txt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_txt.Click
    tmpRS232.Set_com_port("COM8", 38400)
    tmpRS232.Transmit_data(txt_tx.Text)
End Sub

Private Sub tmprs232_rx_data_returned(ByVal str_data As String) Handles tmpRS232.rx_data_returned
    txt_rx.Text = str_data 'Cross threading error
    MsgBox(str_data) 'Fires without errors
End Sub

Can someone please provide a code example based on this code?

thanks.

解决方案

You are correct, the issue here is that you are attempting to update a UI element from a non-UI thread (in this case the serial port handler). What you need to do is check if the InvokeRequired flag is set on the control that you are trying to access from the callback. If so that means that you need to marshall your call to the UI thread. You can achieve this by using either Invoke or BeginInvoke from System.Windows.Forms.Control.

Private Delegate Sub SetRxTextCallback(ByVal [text] As String)

Private Sub SetRxText(ByVal [text] As String)
    txt_rx.Text = [text]
End Sub

Private Sub tmprs232_rx_data_returned(ByVal str_data As String) Handles tmpRS232.rx_data_returned

    If (txt_rx.InvokeRequired) Then
        Dim d As New SetRxTextCallback(AddressOf Me.SetRxText)
        Me.BeginInvoke(d, New Object() {[str_data]})
    End If

    'txt_rx.Text = str_data 'Cross threading error
    'MsgBox(str_data) 'Fires without errors
End Sub

Here's a link to the MSDN documentation that explains it in detail.

这篇关于从类跨线程调用?困惑-vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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