End the program after closing MessageBox

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()); 
+6
source share
4 answers

Since this is in the Main() routine, just return:

 try { reader = new Reader("COM6"); } catch { MessageBox.Show("No Device Detected", MessageBoxButtons.OK, MessageBoxIcon.Error) return; // Will exit the program } Application.EnableVisualStyles(); //... Other code here.. 

Returning from Main() will end the process.

+5
source

Application.Exit tells your WinForms application to stop sending a message, so exit the program. If you call it before calling Application.Run , the message pump never started in the first place, so it freezes.

If you want to terminate your program, no matter what state it is in, use Environment.Exit .

+6
source

Add boolean to the top of the page to determine if the operation is complete.

 bool readerCompleted = false; try { reader = new Reader("COM6"); readerCompleted = true; } catch { MessageBox.Show("No Device Detected", MessageBoxButtons.OK, MessageBoxIcon.Error) } if(readerCompleted) { Application.EnableVisualStyles(); Application.SetCompatibleRenderingDefault(false); Application.Run(new Form1()); } 

Since there is no code after the if , the program will simply close when the boolean is false.

You can apply this logic to any other section of your code, and not just to the Main() function.

+2
source

You can put Application.Exit () after the code of your message code
catch
{
MessageBox.Show("No Device Detected", MessageBoxButtons.OK, MessageBoxIcon.Error")
Application.Exit();
}

0
source

Source: https://habr.com/ru/post/947780/


All Articles