At the very beginning of my program, I check if I can initiate a connection to the device on COM6. If the device is not found, I want to display a MessageBox, and then completely complete the program.
Here is what I have so far used in the Main() function of the source program:
try { reader = new Reader("COM6"); } catch { MessageBox.Show("No Device Detected", MessageBoxButtons.OK, MessageBoxIcon.Error) } Application.EnableVisualStyles(); Application.SetCompatibleRenderingDefault(false); Application.Run(new Form1());
When I try to put Application.Exit(); after the MessageBox command, the MessageBox is displayed correctly when the device is not detected, but when I close the MessageBox, Form1 is still open, but completely frozen and will not allow me to close it, or press any of the buttons that should give me an error since the device is not connected .
I'm just looking to completely kill a program after displaying a MessageBox. Thanks.
SOLUTION: After using the return; method return; after closing MessageBox, the program terminates the way I wanted when the device was not connected. However, when the device was connected, it still had problems after testing. It was something that I had not found before, but it was a simple solution. Here is my fully working code:
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());
source share