Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try
- {
- PleaseDisposeMe a = new PleaseDisposeMe();
- throw new Exception();
- a.Dispose();
- }
- catch (Exception ex)
- {
- Log(ex);
- }
- // This next call will throw a time-out exception unless the GC
- // runs a.Dispose in time.
- PleaseDisposeMe b = new PleaseDisposeMe();
- class MyDisposable: IDisposable {
- public void Dispose() {
- lock(this) {
- if (disposed) {
- return;
- }
- disposed = true;
- }
- GC.SuppressFinalize(this);
- // Do actual disposing here ...
- }
- private bool disposed = false;
- }
- ~ClassName()
- {
- }
- public class DisposableFinalisableClass : IDisposable
- {
- ~DisposableFinalisableClass()
- {
- Dispose(false);
- }
- public void Dispose()
- {
- Dispose(true);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (disposing)
- {
- // tidy managed resources
- }
- // tidy unmanaged resources
- }
- }
- using (SqlConnection con = new SqlConnection("DB con str") )
- using (SqlCommand com = new SqlCommand( con, "sql query") )
- {
- //now code is indented one level
- //technically we're nested twice
- }
- PleaseDisposeMe a;
- try
- {
- a = new PleaseDisposeMe();
- throw new Exception();
- }
- catch (Exception ex) { Log(ex); }
- finally {
- //this always executes, even with the exception
- a.Dispose();
- }
Add Comment
Please, Sign In to add comment