当特定字符从serialport接收时关闭当前正在运行的表单 [英] Closing current running Form when a particular character is receiving from serialport

查看:81
本文介绍了当特定字符从serialport接收时关闭当前正在运行的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..

当我写下面的代码时,我收到了这个错误。



Hi..
When I write the following code I got this error.

 byte[] byte_buffer = new byte[20];

 private void serialPort1_DataReceived(objectsender,System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            int bytes = serialPort1.BytesToRead;
           
            serialPort1.Read(byte_buffer, 0, bytes);

            count++;
            if (byte_buffer[0] == startup)
            {
               
                 this. Close();//Error
                Application. Run(new Form4());
               
               
              }
}           







Cross - 线程操作无效:控制从其创建的线程以外的线程访问的Form1。

如何纠正此错误。我在谷歌搜索过。但我是这个区域的新手。谁能告诉我答案?




Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.
How can I rectify this error.? I searched in Google..But am new to this area.so can anyone tell me the answer?

推荐答案

参见如何:对Windows窗体控件进行线程安全调用 [ ^ ] MSDN page。
See the "How to: Make Thread-Safe Calls to Windows Forms Controls"[^] MSDN page.


这篇关于当特定字符从serialport接收时关闭当前正在运行的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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