“跨线程操作无效” ..在串行端口上读取数据时
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用主窗体上的 Invoke 方法。您必须将控制权传递给主窗体才能与其交互。事件处理程序在后台线程上触发。
以下是一些可能有效的示例代码:
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:
您的事件处理程序未在 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.
这只是因为 Windows 中的线程命令是“除了 UI 线程之外,不得从任何其他线程访问 UI”,
因此您需要使用 Control.Invoke 在 UI 线程上运行访问 UI 的代码。
一点 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.
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