“跨线程操作无效” ..在串行端口上读取数据时

发布于 2024-10-26 22:58:34 字数 992 浏览 1 评论 0原文

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

所以我使用串口的DataReceived事件处理程序。

  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’。”

我尝试删除代码行: Main.Instance.customerData.MdiParent = Main.Instance; 然后就可以正常工作了。但还必须分配 mdiparent 才能将其作为子窗体打开。

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

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.

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();


                }

        }

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."

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听不够的曲调 2024-11-02 22:58:34

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

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

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();
    }
}

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();
    }
}
奈何桥上唱咆哮 2024-11-02 22:58:34

您的事件处理程序未在 UI 线程上运行。要进入 UI 线程,请使用主窗体的 Invoke 方法。

Your event handler is not running on the UI thread. To get on the UI thread use the Invoke method of the main form.

看轻我的陪伴 2024-11-02 22:58:34

这只是因为 Windows 中的线程命令是“除了 UI 线程之外,不得从任何其他线程访问 UI”,

因此您需要使用 Control.Invoke 在 UI 线程上运行访问 UI 的代码。

//assuming your within a control and using C# 3 onward..
this.Invoke( () =>
 {
   //anything that UI goes here.
 }
);

一点 Stackoverflow Ninja Google 搜索 会对你有所帮助。这恰好是一个非常臭名昭著的问题。

这个答案似乎几乎正是你的问题:
侦听时跨线程操作无效COM端口

That is simply because a threading commandment in windows is "Thou shalt not access UI from any other thread but the UI thread"

So you need to use Control.Invoke to run code that Accesses UI on the UI thread.

//assuming your within a control and using C# 3 onward..
this.Invoke( () =>
 {
   //anything that UI goes here.
 }
);

A little Stackoverflow Ninja Google Search Would have helped you out. This happens to be a pretty infamous problem.

This answer seems to be almost exactly your problem:
Cross-thread operation not valid while listening to a COM port

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文