Results 1 to 12 of 12

Thread: Best Practice?

  1. #1

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Best Practice?

    I have read a lot of threads that discuss the dispose method, but I haven’t seen any that identify a best practice. Perhaps I didn’t look hard enough. What I don’t understand is how you know when something needs to be disposed. Should you make it a habit to dispose everything that is used by a subroutine that may be used more than once in the lifecycle of the application? Should you also set all of your variables to Nothing so they can be recovered by the GC sooner?

    I have an MDI application that at any given time will only have one child open. However, the user has the option of opening any one of over 30 possible children. I instantiate the child in a manner such as:

    Code:
    Private Sub uiSubComboBox_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiSubComboBox.SelectedValueChanged
    
    ' CLOSE ANY OPEN MDI CHILDREN
    Dim frm As Form
    For Each frm In Me.MdiChildren
        frm.Close()
    Next
    ' DISPOSE FRM OBJECT
    frm.Dispose()
    
    'DETERMINE CONTENTS OF SUB COMBOBOX
    If Me.uiMainComboBox.Text = "Bill of Material (BOM)" Then
        If Me.uiSubComboBox.Text = "Maintenance" Then
             Dim bomMaint As New bomMaintForm
             bomMaint.MdiParent = Me
             bomMaint.BlendPanel1.Visible = False
             bomMaint.Show()
             bomMaint.Dock = DockStyle.Fill
             bomMaint.BlendPanel1.Visible = True
             bomMaint = Nothing
    As you can see, I have disposed the frm object after the loop because it is no longer needed in the subroutine and will be called many times throughout the lifecycle of the application (Each child is disposed by calling the Close method). Furthermore, I have set the bomMaint variable equal to Nothing after using it to open the child form. As I understand it, this disassociates the child form from the variable, allowing the variable to be picked up by the GC.

    In this next example, the routine is simply checking the database for restocking triggers. This routine is called every 15 seconds by a timer. At the bottom of the routine, I have disposed my connection object and my command object. I have also set my variables equal to Nothing.

    Code:
    Private Sub CheckTriggers()
    
            Try
    
                ' CREATE CONNECTION STRING
                Dim invConString As String = _
                        "Provider=Microsoft.Jet.OLEDB.4.0;Data " & _
                        "Source=" & mdbLocation & _
                        "Jet OLEDB:Database Password=******;"
    
                ' CREATE DATABASE CONNECTION AND OPEN
                invCon = New OleDbConnection(invConString)
                invCon.Open()
    
                ' CREATE COMMAND
                invCmd = New OleDbCommand("SELECT * FROM invMain WHERE (Inventory <= Trigger)", invCon)
    
                ' EXECUTE READER
                invRdr = invCmd.ExecuteReader
    
                If invRdr.HasRows Then
                    Me.uiTriggerButton.Enabled = True
                    Me.uiTriggerButton.Visible = True
                    Me.PictureBox1.Visible = True
                    Me.PictureBox2.Visible = False
                Else
                    Me.uiTriggerButton.Visible = False
                    Me.uiTriggerButton.Enabled = False
                    Me.PictureBox1.Visible = False
                    Me.PictureBox2.Visible = True
                End If
    
                ' CLOSE CONNECTIONS
                invRdr.Close()
                invCon.Close()
    
                ' DISPOSE OBJECTS
                invCon.Dispose()
                invCmd.Dispose()
                invConString = Nothing
                invRdr = Nothing
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
        End Sub
    So my question to you is “What is the best practice?” Am I even doing this correctly? I have searched for tutorials that deal with disposing objects but have found very few that relate the information to a project that is easy to understand. Surely there must be a good way to learn the best practices of memory management other than reading the MSDN articles.

    All responses are appreciated.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Best Practice?

    Hi,

    Here's a lik about the IDisposable.Dispose Method;

    http://msdn.microsoft.com/library/de...sposetopic.asp

    Hope it helps,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Best Practice?

    Sparrow,

    I think you missed the last couple sentences of my post. I have already found all of the MSDN articles. Have you read them yourself? The one you directed me to actually doesn't tell you anything about applying it to your code. It simply gives you the code for the method. Thanks for your reply though.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  4. #4
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Best Practice?

    Don't quote me on this but doesnt the GC run after you exit the method?

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  5. #5

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Best Practice?

    The way I understand it, the GC runs routinely.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  6. #6
    Addicted Member
    Join Date
    Jan 2002
    Location
    West Virginia
    Posts
    193

    Re: Best Practice?

    Doing a google search I found these two (there are more) that says to use Dispose all the time. One states that it is only for unmanaged code. They both say to use it.
    http://www.irritatedvowel.com/Progra...Practices.aspx
    http://weblogs.asp.net/pwilson/archi.../20/77435.aspx

  7. #7

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Best Practice?

    So what constitutes unmanaged code?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  8. #8
    Addicted Member
    Join Date
    Jan 2002
    Location
    West Virginia
    Posts
    193

    Re: Best Practice?

    Don't know. Maybe one of the guru's will clue us in.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Best Practice?

    From MSDN...
    unmanaged code (n.)
    Code that is executed directly by the operating system, outside the Microsoft .NET Framework's common language runtime. Unmanaged code must provide its own memory management, type checking, and security support, unlike managed code, which receives these services from the common language runtime. Unmanaged code must be executed outside the .NET Framework.
    An API call would be a good example of unmanaged code.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Addicted Member
    Join Date
    Jan 2002
    Location
    West Virginia
    Posts
    193

    Re: Best Practice?

    Thanks,

  11. #11

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Best Practice?

    So then would you say that my examples above are correct? Slightly correct? Completely wrong? What would you do different?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  12. #12

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Best Practice?

    So far is still appears as though we are left with vague answers.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width