stop program and release resources

jassie

Well-known member
Joined
Nov 13, 2012
Messages
61
Programming Experience
1-3
I have a C# 2008 windows/console application that I need to support that never seems to stop running. This application
seems to not release resources also. This application does not close connections also.
Thus can you show me in code, tell me, and/or point me to a reference that will answer the following questions:
1. How can I force a program to stop running?
2. How can I have the application release resources?
3. How can I have the appliction close connections?
4. Of the steps listed above, can you tell me the order of how these ending operations should occur?
 
I'm not sure that you could be much more vague if you tried. Firstly, what connections are you talking about? Secondly, to close a console app you simply let the Main method complete, as with any Windows app. The only reason that I can think of that that would not end the process is that there is still one or more foreground threads running. If there are no foreground threads running then the process exits and all resources are released. If that's not happening in your case then it's something specific to what you have done but, as we know nothing about that, we can't provide any advice on how to fix it.
 
Before a program is about ready to stop executing, I am wondering if you can tell what objects(s) are still open? If so, where is visual studio 2008, can I tell what objects are stillopen?
 
There is no such thing as an "open" object. Some types have a concept of "open" when they provide a path for data to flow between your app and some other entity, e.g. streams, database connections, etc. Other objects may hold system resources but can't be considered "open". Every object occupies space in memory and that is managed by the system. There are a few simple rules that you should obey to make sure that the system can automatically reclaim memory when it needs to and that you always release all system resources.

1. Any objects that implement the IDisposable interface should have their Dispose method (or equivalent, e.g. Close) called when you're done with them.
2. If a disposable object is used within a single scope, i.e. created and destroyed within the same block, then use a 'using' statement to create it so that it is implicitly disposed at the end of the block.
3. If a variable remains in scope for some time after you have finished with the object it refers to, set the variable to null.

If you're having a specific issue then you are doing something specific wrong. If you're not prepared to explain exactly what you're doing and exactly what's happening then we probably can't help.
 
Back
Top Bottom