Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to check object life using “Weak Reference class” in VB and C# .net.

Author: Maliha Atteeq
Download Source Code : 943_soruce.zip

In this simple article you will learn that how we can check object life using “Weak Reference class” in VB and C# .net.

 Object Life:

In .net a garbage collector is run after a specific period of time and it collects the objects which have performed there work or whose scope has been finished. Garbage collector basically destroys those objects and frees the memory. To check whether the object has been destroyed by garbage collector after its scope or not for this purpose .net provides us a class named weak reference. When you create an object of this class you pass that object for which you want to check the life.

Constructor:

This class has two constructors in one it takes the object as parameter about which you want to know the life time. And in second it takes the object as first parameter and Boolean value as second parameter. This Boolean value decides whether the object reference should be tracked after the finalize runs or not. True means object reference should be tracked and false tells that object reference should not be tracked.

Target:

If you do not want to change your object about which you want to know its life then we have a property named target. This property takes the object and also returns the object about which you want to know the life time.

Is Alive:

To check object life we have a property named is alive. This property returns the Boolean value, if the value is true it means that the object is alive and garbage collector still not collected it but if the value is false it means that the object has been destroyed and collected by garbage collector.

To demonstrate make a window application. Drag one button on form.

C#

private void btn_tellInfunction_Click(object sender, EventArgs e)

        {

            string str = "devasp";

            obj = new WeakReference(str,true);

            bool b= obj.IsAlive;

            if (b == true)

                MessageBox.Show("OBJECT IS ALIVE");

            else

                MessageBox.Show("OBJECT HAS DESTROYED");

        }

VB

Private Sub btn_tellInfunction_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim str As String = "devasp"

        obj = New WeakReference(str, True)

        Dim b As Boolean = obj.IsAlive

        If b = True Then

            MessageBox.Show("OBJECT IS ALIVE")

        Else

            MessageBox.Show("OBJECT HAS DESTROYED")

        End If

    End Sub

This simple code tells whether the garbage collector has collected the object or not.

Track Resurrection:

To check after the finalize runs whether the object reference was tracked or not. For this purpose we have property named is resurrection. This property returns the Boolean value. If the value is true it means that object is tracked but if it is false then it means it did not track after finalize ran. Object reference should be tracked or not it is decided at initialization time by using overloaded constructor as discussed above.

To demonstrate make a window application. Drag one button on form.

C#

private void btn_Track_Click(object sender, EventArgs e)

        {

            if (obj != null)

            {

                bool b = obj.TrackResurrection;

                if (b == true)

                    MessageBox.Show("OBJECT TRACKED");

                else

                    MessageBox.Show("OBJECT NOT TRACKED");

            }

            else

                MessageBox.Show("OBJECT NOT INITIATED");

            }

VB

Private Sub btn_Track_Click(ByVal sender As Object, ByVal e As EventArgs)

        If obj IsNot Nothing Then

            Dim b As Boolean = obj.TrackResurrection

            If b = True Then

                MessageBox.Show("OBJECT TRACKED")

            Else

                MessageBox.Show("OBJECT NOT TRACKED")

            End If

        Else

            MessageBox.Show("OBJECT NOT INITIATED")

        End If

    End Sub

This simple code tells whether the object has been tracked after the finalize ran or not.

Now write the following code on FORM LOAD event:

C#

        private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "Devasp WeakReference Application";          

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "Devasp WeakReference Application"

    End Sub

This simple article tells that how we can check object life using “Weak Reference class” in VB and C# .net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to handle divide by zero exception using “DivideByZeroException” class in VB and C# (.net).

Disclaimer - Privacy
© 2002-2017 DevASP.net