Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I thought that my program had closed - it hadn't !!

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
This may be something simple that I'm just missing the point on.
I have a C#/.Net application that works against a number of databases and makes entries / updates to a SQL Server database.
I emptied my target database and started my application (run-time rather than through the design editor). Shortly thereafter I remembered that I still had a mistake in the code, so promptly chose to close my application (from the task bar - I have Windows 2000).
I had assumed that my application had then ended - it was not visible in the 'Application' tab within 'Windows Task Manager' and not was visibly running on the desktop.
I set about the emptying of the database table again - successfully.
I then did a COUNT against the tables to ensure that they were empty (I'm a stickler for being careful) and found that there were now entries in the tables.
It appeared that my application was still running - and entries being made / updated to my SQL Server database tables.
The application was then found to be running through the 'Processes' tab within 'Windows Task Manager'.

My question (at long last I get to the point ...) is how do I ensure that when I close the application (through whatever means) all the processes associated with it are stopped and my application is actually killed.

This may be a simple issue with an obvious answer (I'm hoping so) but I can't see it for looking.

Thanks in advance.
Steve
 
Sounds like your classes need to implement the IDispose method.

Recall that C# is a garbage-collected language, and sometimes a GC cycle doesn't occur for minutes after you think you've freed your object.

Chip H.
 
Chiph :
I think that I've solved my own problem on this own (through neccessity) in the sense that I've put the syntax :
Environment.Exit(0);
Application.Exit();
behind the 'Form1_Closing' method of the main form - this seems to have done the trick in the sense that the SQL Server database postings have now stopped when I close the main form.

As for the suggestion with the garbage-collection : How would I make use of this to solve this issue (albeit already solved) ?

Thanks again.
Steve
 
Even after you've called the .Close method on your database connection, the object still exists until the Garbage Collector gets around to actually disposing of it. You can force this to happen by calling the .Dispose method yourself after closing it.

This isn't the most efficient way to do things in .NET (it's best to let the GC do it's stuff), but when you've got an expensive resource like a database connection, it's a good idea to ensure it's freed as soon as you're done with it.

If you're writing an EnterpriseServices component, other factors enter into the situation (aka connection pooling), but in general you'd still call .Dispose on it.

Chip H.
 
So I need to call either .Close or .Dispose against my SqlConnection ?
Which is favourable ?
Do I place this call behind the (main) form Close / Closing / Closed event ?

Thanks for the feedback.
Steve
 
I can't find a reference that talks about it, but it seems to me that application termination would be the perfect time for a garbage collection. So much so that I would be surprised if there was not one.
 
StevenK -
You need to call both - the .Close to close the connection, and the .Dispose to free it up.

Rosenk -
I saw a book at the bookstore the other day that may address this issue -- Inside C#, 2nd Ed. It goes into the details of how the language works. Don't actually recall if this subject was in there or not, though.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top