0

I have a large application with an explicit Application.Exit() call after a confirmation dialog. However, if I debug that application and I quit, the application doesn't close. Visual Studio still says "Running". All windows are closed.

Is there a way to find out what part of the application is still running or blocking something? I tried "Break All", but it didn't give me any useful info. Also, I have only one process and no threads.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • http://stackoverflow.com/questions/1057151/application-exit – Ian P Dec 04 '15 at 14:52
  • Have you tried using IntelliTrace and looking at the call stack? – SierraOscar Dec 04 '15 at 14:52
  • You may have threads that are still running. Try using `Environment.FailFast` or `Environment.Exit`. I'd suggest fixing the problem though, look at the Threads window when you exit, see which ones are still running. You should be able to pause them and see more details about what they are doing. – Ron Beyer Dec 04 '15 at 14:52
  • @RonBeyer obviously I want to fix the problem, but I have no idea where to look. – Bart Friederichs Dec 04 '15 at 14:52
  • Like I said, try looking at the threads window. You may not be explicitly creating them, but something you are using may have spun one off. – Ron Beyer Dec 04 '15 at 14:53
  • @RonBeyer I found the issue. I didn't close the database connection. Apparently a small change in it caused it to not close automatically as it did before. – Bart Friederichs Dec 04 '15 at 14:55
  • You should wrap database connections in `using` statements to avoid that kind of problem in the future, glad you found it though. – Ron Beyer Dec 04 '15 at 15:01
  • @RonBeyer how would I do that if I want to keep a connection open during the entire time the application is running? – Bart Friederichs Dec 04 '15 at 15:02
  • Thats a different use case I guess and wouldn't fit that pattern. Typically you want to give up connections as soon as you are finished with them, I didn't see that you were using notifications until you posted your answer. – Ron Beyer Dec 04 '15 at 15:05
  • @BartFriederichs keeping the connection open during the entire time the application is running sounds like a bad idea to me – Sybren Dec 04 '15 at 15:05
  • @Sybren large part of the application is `DataGridViews` showing data from the database. Having to reconnect every time (which includes a trip to a license server in my case) a person presses F5 seems worse. – Bart Friederichs Dec 04 '15 at 15:38

2 Answers2

1

I found the issue, it was an unclosed database connection. I added SyncNotification=true to the connection string, and apparently this also means the connection needs to be closed explicitly.

So, for future visitors: as other people have mentioned: check the threads, but also any connections or other possibly blocking objects.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0

According to the documentation Application.Exit informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. Is any code still running in the UI thread ?

Alex
  • 21,273
  • 10
  • 61
  • 73