0

I used to call Application.Exit terminate my application. At some point in time, I start to use a DLL, and I cannot fully terminate my application (a thread is running).

However, I can call Environment.Exit(0) and terminate my app completely.

I have read some useful posts.

[Closing Applications

[http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx][2]

It seems Application.Exit() is recommended for Windows application and Environment.Exit(exitCode) is recommended for console application

That's what I read from other posts.

===== The reason that I asked this question, and Something is still unclear to me:

Now, I just add Environment.Exit(0) at the end of Application.Exit event handler, since I had the event handler for Application.Exit and save some state information in the handler.

I am still not sure that I use this in a correct way. In other words, does this make sense to call both Application.Exit() and Environment.Exit(exitCode)

Community
  • 1
  • 1
Boyang
  • 93
  • 1
  • 6

1 Answers1

1

If you really just needs to terminate the App instantly and terminate all the threads, then you need to use Environment.Exit(0).

If you want your Forms' Close events to fire and your threads to continue their work, you need to use Application.Exit();

It really depends on your situation. The first is like you terminating the process (Killing it). The Second is like sending a message to the App and politely asking it to finish everything in hand and then close for good. This allows to save some state information by firing the closing events if implemented.

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • Thank you for the answer. That's what I read from other posts. Something unclear to me is : Now, I just add `Environment.Exit(0)` at the end of `Application.Exit` event handler, since I had an event for `Application.Exit` and save some state information in the handler. I am still not sure that I use this in a correct way. In other words, does this make sense to call both `Application.Exit()` and `Environment.Exit(exitCode)` – Boyang Jul 18 '16 at 18:48
  • @Boyang for me, it doesn't make any sense. You can save whatever state you want and then call `Environment.Exit(exitCode)` directly without the need to call `Application.Exit();` – Zein Makki Jul 18 '16 at 19:07