线程化与rtb交互的方法 [英] threading a method which interacts with an rtb

查看:83
本文介绍了线程化与rtb交互的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我是线程的新手,我得到了这个概念并且看到了很多我理解的示例代码。我当时的比喻是,线程就像在办公室里雇员一样。每个人都会离开并同时做一些工作,并可以在何时报告。



无论如何,我做了一个简单的程序来读取一些数据。 com端口。然后解析数据并吐出到richtextbox中。鉴于数据量有时可能有点资源密集,因此我想将此方法放在自己的主题中。



我试过这样做,使用类似的代码到下面,但继续被VS告知,因为我正在从一个允许的线程之外的线程与rtb进行交互。



Ok, so i''m new to threading, I get the concept and have seen plenty of example code which I understand. My analogy at the minute is that threading is like having employees in an office. Each one goes away and does some work at the same time and can report back as and when.

Anyway, I have made a simple program to read some data from a com port. The data is then parsed and spat out into a richtextbox. Given the volume of data this may be somewhat resource intensive at times and therefore I''d like to put this method in its own thread.

I tried this as such, using similar code to the following, but keep getting told off by VS because I''m interacting with the rtb from a thread outside of what is allowed.

Private Sub btn_Restart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Restart.Click
    ' restart button
    Call restart()
End Sub







Private Sub restart()
            
            ' do some error checking
            ' and other stuff

            ' threading attempt
            Dim d As myDelegate = AddressOf ReceiveSerialData
            d.BeginInvoke(cmb_Com.Text, rtb_ComData, Nothing, Nothing)

            ' this is what I previously had to call the method before
            ' I wanted to use threading...
            'ReceiveSerialData(cmb_Com.Text, rtb_ComData)

    End Sub

    Delegate Sub myDelegate(ByVal comport As String, ByRef rtb As RichTextBox)

    Sub ReceiveSerialData(ByVal comport As String, ByRef rtb As RichTextBox)
        Try
            ' Receive sentences from a com port 
            ' spit the data out to the rtb
        Catch ex As TimeoutException
            rtb.Text = "Error: Com Port read timed out."
            Me.pic_Data.BackColor = Color.Red
            Me.pic_Checksum.BackColor = Color.Red
            btn_Restart.Enabled = True
        Catch ex As Exception
            rtb.Text = "something went wrong, try again."
            Me.pic_Data.BackColor = Color.Red
            Me.pic_Checksum.BackColor = Color.Red
            btn_Restart.Enabled = True
        Finally
            If com IsNot Nothing Then com.Close()
            Me.pic_Data.BackColor = Color.Red
            Me.pic_Checksum.BackColor = Color.Red
            btn_Restart.Enabled = True
        End Try
    End Sub





这很容易将代码转移到一个线程?任何帮助/指针都很有必要。



干杯



Is this easy to "transfer" the code to a thread? Any help / pointers much obliged.

Cheers

推荐答案

您无法从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于线程化与rtb交互的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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