|
-
Oct 31st, 2006, 11:25 AM
#1
Thread Starter
Frenzied Member
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.
-
Oct 31st, 2006, 11:29 AM
#2
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
-
Oct 31st, 2006, 11:50 AM
#3
Thread Starter
Frenzied Member
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.
-
Oct 31st, 2006, 11:53 AM
#4
Fanatic Member
Re: Best Practice?
Don't quote me on this but doesnt the GC run after you exit the method?
-
Oct 31st, 2006, 12:05 PM
#5
Thread Starter
Frenzied Member
Re: Best Practice?
The way I understand it, the GC runs routinely.
-
Oct 31st, 2006, 01:11 PM
#6
Addicted Member
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
-
Oct 31st, 2006, 01:23 PM
#7
Thread Starter
Frenzied Member
Re: Best Practice?
So what constitutes unmanaged code?
-
Oct 31st, 2006, 01:50 PM
#8
Addicted Member
Re: Best Practice?
Don't know. Maybe one of the guru's will clue us in.
-
Oct 31st, 2006, 02:00 PM
#9
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Oct 31st, 2006, 02:05 PM
#10
Addicted Member
-
Oct 31st, 2006, 02:12 PM
#11
Thread Starter
Frenzied Member
Re: Best Practice?
So then would you say that my examples above are correct? Slightly correct? Completely wrong? What would you do different?
-
Oct 31st, 2006, 03:58 PM
#12
Thread Starter
Frenzied Member
Re: Best Practice?
So far is still appears as though we are left with vague answers.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|