1

I'm writing an interface that extract information from TAPI packages and sends them through modbusTCP packages to a modbusclient.

Due to some library limitations, I had to write a code which exits application if there is no network connection even before loading any windows forms or GUI.

And since Application.exit() raises formclosing event and do nothing else (as far as I know), I can not use this method (I tried and it failed to exit application).

How can exit application safely?

David Arno
  • 42,717
  • 16
  • 86
  • 131
ltc0060
  • 11
  • 2
  • Don't call the form constructor 'new form1()' until you get a connection. – jdweng May 06 '15 at 13:22
  • *Application.Exit() raises formclosing event and do nothing else* Wrong. It terminates the message loop. – Sriram Sakthivel May 06 '15 at 13:23
  • You can simply return from the Main method if you know there itself. – Sriram Sakthivel May 06 '15 at 13:24
  • Application.Exit() can only work when your program has progressed far enough and entered the Application.Run() call. That probably did not happen yet, use the debugger to verify. Environment.Exit() is the alternative. – Hans Passant May 06 '15 at 13:31
  • @HansPassant Environment.exit() worked, thanks. But I dont understand the difference. In another post http://stackoverflow.com/questions/13046019/winforms-application-exit-vs-enviroment-exit-vs-form-close it says *resources (database connections etc.) could not be released properly*, but I need sockets to be be released. – ltc0060 May 07 '15 at 05:38
  • That post is popular because it sounds good, it is not accurate. Environment.Exit() still runs the finalizer of all .NET objects, including those of any sockets you might have opened. – Hans Passant May 07 '15 at 06:40

0 Answers0