在serialport datareceived事件中关闭表单 [英] Closing form in serialport datareceived event

查看:66
本文介绍了在serialport datareceived事件中关闭表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..

如何从串口接收数据时关闭当前运行的表单。我试过背景工作者,Involk()等。但我不能关闭这个表格。怎么可能?



Hi..
How to close a current running form when a data received from serial port. I tried background worker , Involk() etc. But I cant close this form. How it possible?

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
       {
           int bytes = serialPort1.BytesToRead;

           serialPort1.Read(byte_buffer, 0, bytes);

           count++;
           if (byte_buffer[0] == startup)
           {

               Form1 f = new Form1();
               Form4 f2 = new Form4();
               f2.Show();

               //this.Close();
           }
       }



这是我的代码


this is my code

推荐答案

你可以关闭这个表格 - 这很容易,请致电关闭:

You can close "this form" - it's easy, just call close:
f2.Show();
Close();

但是......如果当前表单是主表单,那将始终关闭应用程序 - 终止SerialPort和Form2。



所以你可能要做的就是隐藏这个表单,并在Form2执行时关闭它:

But...if the current form is the main form, that will always close the application - which terminates the SerialPort, and Form2.

So probably what you want to do is to Hide this form, and close it when Form2 does:

f2.FormClosed += new FormClosedEventHandler(f2_FormClosed);
f2.Show();
Hide();



然后:


Then:

void f2_FormClosed(object sender, FormClosedEventArgs e)
    {
    Close();
    }


这篇关于在serialport datareceived事件中关闭表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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