"跨线程操作无效".. 在串口上读取数据时 [英] "Cross thread operation not valid" .. while reading data on Serial Port

查看:27
本文介绍了"跨线程操作无效".. 在串口上读取数据时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows 窗体应用程序中,在主窗体加载时,我设置了一个串行端口并开始读取它.目的是,当我在串口上接收到一些数据时,我想打开另一个与数据相关的表单.

In a windows form application, on main form load, i have set a serial port and started reading it. The purpose is, as and when I receive some data on the serial port, I want to open another form related to the data.

所以我使用串口的DataReceived Event Handler.

So i use the DataReceived Event Handler of the serial port.

  void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

            string str = this.serialPort1.ReadLine();


                if (!string.IsNullOrEmpty(str))
                {


                    Main.Instance.customerData = new CustomerData(str);
                    Main.Instance.customerData.MdiParent = Main.Instance;  //Exeption received at this point
                    Main.Instance.customerData.Disposed += new EventHandler(customerData_Disposed);

                    Main.Instance.customerData.Show();


                }

        }

但是当我尝试在事件处理程序中打开一个表单时,它给了我一个 InvalidOperationExeption 说:跨线程操作无效:控制 'Main' 从创建它的线程以外的线程访问."

But when I try to open a form within the event handler it gives me an InvalidOperationExeption saying: "Cross-thread operation not valid: Control 'Main' accessed from a thread other than the thread it was created on."

我尝试删除代码行:Main.Instance.customerData.MdiParent = Main.Instance;然后它工作正常.但它也有必要分配 mdiparent 以便将其作为子窗体打开.

I tried removing the code line : Main.Instance.customerData.MdiParent = Main.Instance; then it works fine. But its necessary also to assign the mdiparent in order to open it as a child form.

有什么解决这个问题的建议吗?

Any suggestions to resolve this problem ?

推荐答案

在 Main 窗体上使用 Invoke 方法.您必须将控制权传递给 Main 窗体才能与之交互.事件处理程序在后台线程上触发.

Use the Invoke method on the Main form. You have to pass control over to the Main form to interact with it. The event handler is triggered on a background thread.

以下是一些可能有效的示例代码:

Here's some sample code that may work:

void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    string str = this.serialPort1.ReadLine();
    if (!string.IsNullOrEmpty(str))
    {
        ShowCustomerData(str);
    }   

}

private delegate void ShowCustomerDataDelegate(string s);

private void ShowCustomerData(string s)
{
    if (Main.Instance.InvokeRequired)
    {
        Main.Instance.Invoke(new ShowCustomerDataDelegate(ShowCustomerData), s);
    }
    else
    {

        Main.Instance.customerData = new CustomerData(str);
        Main.Instance.customerData.MdiParent = Main.Instance;  //Exeption received at this point
        Main.Instance.customerData.Disposed += new EventHandler(customerData_Disposed);

        Main.Instance.customerData.Show();
    }
}

这篇关于"跨线程操作无效".. 在串口上读取数据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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