views:

44

answers:

1

My GUI desktop-based WPF 4.0 (C# .Net 4.0) program works with SQL Server database. Each time when I run my application it creates connection to SQL Server via ADO.NET Entity Framework and if SQL Server is not reachable it throws exception and shows MessageBox with notification.

Now I want that after user read this message application will shut down. I found two ways to do this:

Process.GetCurrentProcess().Kill();

or

this.Shutdown(); // Application.Current.Shutdown()

or

System.Environment.Exit(0);

Both work fine and do what I need — close application and kill application's process Windows Task Manager.

I want to know:

  1. What is the difference between them?
  2. Which way will close my application faster?
  3. Which way to close application should I use?
  4. Is Application.Current.Shutdown() and this.Shutdown() the same way to close application?

Or may be there is another, more suitable, way to close WPF GUI application (Application.Exit() doesn't work for me, VS2010 returns error — «The event 'System.Windows.Application.Exit' can only appear on the left hand side of += or -=»)?

Thanks.

+3  A: 

The Application.Current.Shutdown() is the proper way to shutdown application. Generally because fire the exit events that You can handle more

The Process.GetCurrentProcess().Kill() should be used when You want to kill application. more

Ad1. The nature of those methods are totally different. The shutdown process can be paused to end some operations, kill force the application to close.

Ad2. Probably Kill will be the festes way, but this is something like kernel panic.

Ad3. Because it fire close events

Ad4. That depend what this is.

Vash
>> Ad4. That depend what this is.I call connection function in App.cs constructor, so «this» — it's instance of App-class.
Toucki