【发布时间】:2013-06-20 19:26:30
【问题描述】:
在我的程序的最开始,我正在检查是否可以启动与 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());
当我尝试在 MessageBox 命令后放置 Application.Exit(); 时,如果未检测到设备,MessageBox 会正确显示,但是当我关闭 MessageBox 时,Form1 仍然打开,但完全冻结,不会让我关闭它或由于设备未连接,请单击任何应该给我错误的按钮。
我只是想在显示 MessageBox 后完全杀死程序。谢谢。
解决方案: 在MessageBox 关闭后使用return; 方法后,程序在未插入设备时按我想要的方式退出。但是,当设备插入时,在测试后仍然存在读取问题。这是我以前没有发现的,但这是一个简单的修复。这是我的完整工作代码:
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", MessageBoxButtons.OK, MessageBoxIcon.Error)
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
【问题讨论】:
标签: c# winforms messagebox