c# – MessageBox关闭后的结束程序
发布时间:2020-12-15 18:08:08 所属栏目:百科 来源:网络整理
导读:在我的程序的最开始,我正在检查是否可以在COM6上启动与设备的连接.如果找不到设备,那么我想显示一个MessageBox,然后完全结束该程序. 这是我到目前为止在初始程序的Main()函数中所拥有的: try{ reader = new Reader("COM6");}catch{ MessageBox.Show("No Dev
|
在我的程序的最开始,我正在检查是否可以在COM6上启动与设备的连接.如果找不到设备,那么我想显示一个MessageBox,然后完全结束该程序.
这是我到目前为止在初始程序的Main()函数中所拥有的: try
{
reader = new Reader("COM6");
}
catch
{
MessageBox.Show("No Device Detected",MessageBoxButtons.OK,MessageBoxIcon.Error)
}
Application.EnableVisualStyles();
Application.SetCompatibleRenderingDefault(false);
Application.Run(new Form1());
当我尝试放一个Application.Exit();在MessageBox命令之后,MessageBox在没有检测到设备的情况下正确显示,但是当我关闭MessageBox时,Form1仍然打开,但是完全冻结,不会让我关闭它或者单击应该给我一个错误的任何按钮无论如何,因为设备没有连接. 我只是想在显示MessageBox之后完全杀掉程序.谢谢. 解决方案:使用返回后;在MessageBox关闭程序后退出的方法就像我想要的那样,当设备没有插入它时.但是,当设备插入时,测试后仍然有读取问题.这是我以前没有发现的东西,但这是一个简单的修复.这是我完全正常工作的代码: try
{
test = new Reader("COM6");
test.Dispose(); //Had to dispose so that I could connect later in the program. Simple fix.
}
catch
{
MessageBox.Show("No device was detected",MessageBoxIcon.Error)
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
解决方法
由于这是在Main()例程中,只需返回:
try
{
reader = new Reader("COM6");
}
catch
{
MessageBox.Show("No Device Detected",MessageBoxIcon.Error)
return; // Will exit the program
}
Application.EnableVisualStyles();
//... Other code here..
从Main()返回将退出该过程. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
