Of course, there's no compile obligation to implement or call Dispose();
but, when dealing with statics; it's a good idea.

What would you expect to happen if a static (or a member of a static) was
given a instance or a delegate of an instance (e.g. the state object or
delegate of the Timer object)?  GC would eventually deal with it; but, at
an appropriate time?  Plus, there's no guarantee that the GC will dispose
of an object (vital for unmanaged resources)  It's not an obligation, but
good form to Dispose as soon as you know you're done with an object.

http://www.peterRitchie.com/

On Mon, 12 Dec 2005 11:59:01 -0500, Eames, Andrew <[EMAIL PROTECTED]>
wrote:

>There's no obligation to Dispose it. If it's a static object then it's
>going to be around for the lifetime of the program anyway so there may
>be no point in Disposing it (unless it's some object where disposing it
>has a side effect like an open stream).
>
>In general, it's better to create and Dispose objects as you need them.
>Disposing of static objects only seems to be useful in relatively few
>scenarios. (e.g. for objects that can be disposed and regenerate
>themselves later)
>  Andrew
>
>-----Original Message-----
>From: Discussion of advanced .NET topics.
>[mailto:[EMAIL PROTECTED] On Behalf Of Peter Ritchie
>Sent: Monday, December 12, 2005 11:12 AM
>To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
>Subject: Re: [ADVANCED-DOTNET] Implementing Dispose on Static(/Shared)
>members
>
>With static member variables (i.e. object-oriented globals) there
>is "automated" dispose pattern (like, "using" in C#).  So, yes, you'll
>have to create and manually call your own static member that disposes of
>your managed resources.
>
>I would suggest avoiding using static member variables and instantiate a
>class (which could be the one-and-only static variable of your startup
>class) that manages the managed resources these variables represent.
>For
>example:
>' converted from C# at
>http://www.carlosag.net/Tools/CodeTranslator/Default.aspx
>
>Namespace PRI.twelve_dec_05
>    Public Class MyClass
>        Private Shared myResources As MyResources
>
>        ' ...
>
>        ' <summary>
>        ' The main entry point for the application.
>        ' </summary>
>        <STAThread()>  _
>        Private Shared Sub Main()
>            ' C# would use the "using" keyword here
>            Try
>                MyClass.myResources = New MyResources
>                ' do processing
>            Finally
>                Form1.myResources.Dispose
>            End Try
>        End Sub
>    End Class
>
>    Public NotInheritable Class MyResources
>        Inherits IDisposable
>
>        Private timer As System.Threading.Timer
>
>        Private disposed As Boolean
>
>        Public Sub New()
>            MyBase.New
>            timer = New System.Threading.Timer(New
>TimerCallback(OnTimer),
>Me, 1000, 1000)
>        End Sub
>
>        Public ReadOnly Property Timer As System.Threading.Timer
>            Get
>                Return timer
>            End Get
>        End Property
>
>        Private Sub OnTimer(ByVal state As Object)
>            ' do something
>        End Sub
>
>        Public Overloads Sub Dispose()
>            Dispose(true)
>        End Sub
>
>        Private Overloads Sub Dispose(ByVal disposing As Boolean)
>            If Not Me.disposed Then
>                If disposing Then
>                    ' dispose managed resources
>                    timer.Dispose
>                End If
>                ' dispose unmanaged resources
>            End If
>            disposed = true
>        End Sub
>    End Class
>End Namespace
>
>Timer is this example is probably awkward (you'd probably want to either
>provide the state object or the delegate to the MyResources
>constructor);
>but, you get the idea.  The MyResources class would contain public
>properties to get access to the various resource instances (e.g. the
>read-
>only Timer property) and would be instantiated as a "singleton" at the
>beginning of the entry-point (Main, in this example) and disposed before
>the exit (using try-finally in VB; but, C# would use "using")
>
>http://www.peterRitchie.com/
>
>On Mon, 12 Dec 2005 19:32:27 +0530, Swaminathan S R <[EMAIL PROTECTED]>
>wrote:
>
>>Hi all,
>>I have an application where i use quite a number of private static
>>variables, as these should not be exposed as instance members. Now to
>>release the resources held by these static members should i write
>>another static member, probably named "releaseResources"  which calls
>>the dispose of the static variable or is there any better way to do
>>this? I know this can be achieved by using a singleton pattern also,
>but
>>how can we do in this way of implementation?
>>
>>--- EXAMPLE:
>>
>>' In the sample shown below is it left to the developer's will to write
>>a method that disposes the timer "tmr".
>>
>>/*************** SAMPLE CODE STARTS ********/
>>Public Class Temp
>>
>>Private Share tmr as Threading.Timer
>>
>>Public Shared Sub Start()
>>    'There should be just an instance of the timer for the application.
>>    tmr = New Threading.Timer(Addressof x, ....)
>>End Sub
>>
>>Public Shared ReleaseResources()
>>    tmr.Dispose()
>>End Sub
>>
>>End Class
>>/*************** SAMPLE CODE ENDS ********/

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to