C# Using statement confusion

Last post 11-08-2009 4:57 AM by binli0114. 5 replies.

Sort Posts:

  • C# Using statement confusion

    11-02-2009, 10:20 AM
    • Member
      point Member
    • khan320
    • Member since 11-02-2009, 3:08 PM
    • Posts 3

    I read that Dispose method is called even if exeception is thrown from within a using block as using gets translated into try/finally clauses by compiler where (in IL) Dispose is being called in finally clause. But in my example code below, when i debug it in Visual Studio, it throws exeception and Dispose never gets called. Please let me know what's wrong here. Thanks a bunch.

    Please note obj.GetType() throws exeception as I'm doing intentionally to see if Dispose gets called or not as menioned above.

    class Datatbase
        {
            public void Commit()
            {
                Console.WriteLine("Commiting database transaction.");
            }
            public void Rollback()
            {
                Console.WriteLine("Rolling back transaction.");
            }
        }

        class Helper : IDisposable
        {
            private bool disposed = false;
            private bool commited = false;
            private Datatbase db;

            public Helper(Datatbase db)
            {
                this.db = db;
            }

            ~Helper()
            {
                Dispose(false);
            }

            public void Commit()
            {
                db.Commit();
                commited = true;
            }

            public void Dispose()
            {
                Dispose(true);
            }

            private void Dispose(bool disposing)
            {
                if (!disposed)
                {
                    if (disposing)
                    {
                        // coming through dispose method.
                        if (!commited) db.Rollback();
                    }
                    else
                    {
                        // coming through finalizer.
                        Console.WriteLine("User forgot to call dispose method.");
                    }
                }
            }
        }

        private static void DoSomething(Datatbase db)
        {
            using (Helper h = new Helper(db))
            {
                obj.GetType();
                db.Commit();
            }
        }

        private static Object obj = null;

     

  • Re: C# Using statement confusion

    11-02-2009, 10:44 AM
    • All-Star
      54,880 point All-Star
    • DarrellNorton
    • Member since 04-04-2003, 3:49 PM
    • VA, USA
    • Posts 6,608
    • Moderator
      TrustedFriends-MVPs

    You have to allow the application to continue running not in debug mode.  For example, the following program prints "Dispose Method" after the exception info is written to the console:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Console1
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (Helper h = new Helper("db"))
                {
                    h.ThrowException();
                }
            }
        }

        class Helper : IDisposable
        {
            private bool disposed = false;
            private bool commited = false;
            private string db;

            public Helper(string db)
            {
                this.db = db;
            }

            public void ThrowException()
            {
                if (db.Length < 5)
                    throw new ArgumentException();
            }

            ~Helper()
            {
                Dispose(false);
            }

            public void Commit()
            {
                commited = true;
            }

            public void Dispose()
            {
                Dispose(true);
            }

            private void Dispose(bool disposing)
            {
                Console.WriteLine("Dispose method");
                if (!disposed)
                {
                    if (disposing)
                    {
                        // coming through dispose method.
                        if (!commited) db.ToString();
                    }
                    else
                    {
                        // coming through finalizer.
                        Console.WriteLine("User forgot to call dispose method.");
                    }
                }
            }
        }
    }

    Darrell Norton, MVP
    Darrell Norton's Blog


    Please mark this post as answered if it helped you!
  • Re: C# Using statement confusion

    11-02-2009, 10:52 AM
    • All-Star
      26,465 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 5:49 AM
    • Mumbai
    • Posts 4,497

    Hi,


    R Using Finally..

    Try

    {

    }

    catch

    {

    }

    Finally

    {

    //Call Ur Dispose Method

    }

    Try calling Ur Dispose Method in finally

    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • Re: C# Using statement confusion

    11-02-2009, 5:39 PM
    • Member
      point Member
    • khan320
    • Member since 11-02-2009, 3:08 PM
    • Posts 3

    How you run the application when "Dispose Method" gets printed?

    Two way I'm been trying is either through Visual Studio or double clicking the exe file for this console application ?

    Input would be appreicated. Thanks

     

  • Re: C# Using statement confusion

    11-07-2009, 12:03 PM
    Answer
    • All-Star
      63,000 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,308
    • TrustedFriends-MVPs

    I suggect you run FXCOp against your compiled code. You can get FXCOP from http://www.microsoft.com/downloads/details.aspx?FamilyID=9aeaa970-f281-4fb0-aba1-d59d7ed09772&DisplayLang=en

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: C# Using statement confusion

    11-08-2009, 4:57 AM
    • Member
      188 point Member
    • binli0114
    • Member since 09-25-2006, 8:31 PM
    • Sydney
    • Posts 48

    Hi mate

    I think you can actually register a callback method within AppDomain type's DomainUnload Event.


    ------------------------------------------------------------
    I LOVE THIS GAME
Page 1 of 1 (6 items)