3

Question as per title.

I have a piece of code that does this:

 using (SqlConnection dbcon = new SqlConnection(connectionString)) using (SqlDataAdapter dataAdapter = new SqlDataAdapter(statement, dbcon)) { dat_set = new System.Data.DataSet(); dbcon.Open(); dataAdapter.Fill(dat_set, name); } 

when I force exit the program when the dataAdapter is still filling the data set, the program freeze and stop responding.

I know "using" block release the resource when they go out of scope, but in the case of force termination, does the resources get release gracefully?

6
  • 1
    the bigger question is : "why are you forcing exit the program when the dataAdapter is still filling"? Commented Sep 29, 2015 at 5:51
  • 1
    Because from the user point of view, they should be able to terminate the program at any point of time. Commented Sep 29, 2015 at 5:57
  • perhaps then, your adapter takes too long to fill. Consider loading a smaller dataset. Perhaps use paging.... Commented Sep 29, 2015 at 6:02
  • if the user wants it closed, then probably you would need to have a separate program that does the data set fill or whatever that fill does. It only means your client program should only be a "caller" of that separate .exe and check its status from time to time. :) 2 cents Commented Sep 29, 2015 at 6:08
  • @MitchWheat yes I am grabbing quite a big amount of data from the database. For test purpose I am querying for top 1000000 from database to dataset. Will you be kind enough to provide a link to an online resource for this paging that you mentioned? Commented Sep 29, 2015 at 6:16

2 Answers 2

7

If a process exits, all the native resources (network handles, file handles etc) are cleared up by the operating system. I wouldn't expect the using statement to come into effect here - I suspect the OS will kill the threads too hard for it to get a chance to do the clean-up itself. That means you'll still end up with the clean-up being done... but any flushing you might expect to happen as part of a using statement (e.g. in a file writing scenario) may not happen.

Sign up to request clarification or add additional context in comments.

3 Comments

if the above code is performed in a thread, does calling thread.abort() have the same result too? i.e operation system enters to perform the clean up of native resource
@gin: No, I believe Thread.Abort will still execute finally statements up the stack. (Heck, you can catch ThreadAbortException and reset it if you really want.) Aborting a single thread doesn't let the OS do any cleanup, because the resource code be in use in a different thread.
I just ran some tests out of curiosity. Thread.Abort, AppDomain.Unload and Enviorment.FailFast all cause the finally block to be run (I was pretty sure Thread.Abort would do it but was fairly surprised about the last two).
1

using statements are just easy ways to do try-finally blocks. So really the question is if you force quit a program will finally blocks run, and the answer is "no".

However, all external handles (including the database connection you opened in your code example) are released when the program terminates, so you will not have a orphaned database connection even though the finally block was never run. When the connection is terminated any transactions that are still open will be rolled back and undone (Even if you don't explicitly start a transaction all requests are wrapped in a implicit transaction).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.