Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "using" block release resource when program force closed

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?

like image 828
gin Avatar asked Jan 25 '26 17:01

gin


1 Answers

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.

like image 190
Jon Skeet Avatar answered Jan 28 '26 17:01

Jon Skeet