Special VARCHART Collections Now support IDisposable Interface

Posted by Klaus Ribbrock on Feb 10, 2015 11:30:00 AM

In case of intensive use, some types of VARCHART collections can lead to a significant rise in occupied storage place if they are not released quickly. The three automation objects VcNodeCollection, VcGroupCollection and VcLinkCollection in VARCHART XGantt now support the .NET Interface IDisposable and explicitely release unmanaged resources.

This blog post instructs developers how to correctly access the collections when using the Gantt component VARCHART XGantt in order to ensure a minimal use of storage place in their application to improve the performance of your Gantt application.

VARCHART XGantt Trick: Improve performance by using IDisposable

As a rule, .NET uses its own mechanism, the Garbage Collector (GC), to quickly delete objects that are not used any more. If, however, these objects have access to other objects that can’t be managed by the GC, deleting will be suspended and only carried out after the application has been closed -  unless the objects are not handled accordingly by the program.

This is the case with the automation objects VcNodeCollection, VcGroupCollection and VcLinkCollection of VARCHART XGantt. Please proceed as described below if you use these collections intensively in your application.

IDisposable from Version 5.0 in VARCHART XGantt

The Collections VcNodeCollection, VcGroupCollection and VcLinkCollection support the IDisposable mechanism from Version 5.0 onward and can be processed correctly with little programming effort.

There are two aspects to be considered in this context:

#1 Events

If the collections are passed in an event, they also have to be used in this context only! From version 5.0 onward, the internal objects of VARCHART XGantt will be removed after having exited the event method and the collections will be ready for deleting in the GarbageCollection. The objects can’t be used any more. Attempts to access will trigger a VcObjectDisposedException.

Wrong

Dim nodeCollection As NETRONIC.XGantt.VcNodeCollection

Dim count As Integer 

Private Sub VcGantt1_VcNodesMarking(sender As System.Object, e As NETRONIC.XGantt.VcNodesMarkingEventArgs) Handles VcGantt1.VcNodesMarking

nodeCollection As NETRONIC.XGantt.VcNodeCollection= e.NodeCollection

End Sub

After having exited the event method the nodeCollection is not valid any more and hence can’t be used any more.

count = nodeCollection.Count

Executing this code line will trigger the aforementioned exception.

 

Correct

If information is to be passed beyond the event method, other data holders will have to take over this task.

Dim count As Integer 

Private Sub VcGantt1_VcNodesMarking(sender As System.Object, e As NETRONIC.XGantt.VcNodesMarkingEventArgs) Handles VcGantt1.VcNodesMarking

nodeCollection As NETRONIC.XGantt.VcNodeCollection= e.NodeCollection

count = nodeCollection.Count

End Sub

#2 Accessing the collections by API

Even in case of a normal access by API, an automation object will always be created and not be released until the application will have been closed:

Dim nodeCollection As NETRONIC.XGantt.VcNodeCollection = VcGantt1.NodeCollection
.
.
Dim count As Integer = nodeCollection.Count

If, e.g., this code will be called 10000 times, then 10000 nodeCollections will be created without being released for the GC.

 

Release unmanaged resources

VARCHART XGantt was enhanced by implementing the interface IDisposable for these objects so that the scope within which to use them can be specified.

This interface is mainly used for releasing unmanaged resources. The GC normally releases the storage used by a managed object if this object is not used any more but it can’t be predicted, however, when the GC will be carried out. Moreover, unmanaged resources such as window handles, open files, streams etc. are unknown to the GC.

With the Dispose method of this interface, unmanaged resources can be released explicitely. Users of an object can call this method if this object is not needed any more.

The time when the object is not needed any more will therefore be specified by the application itself. The unmanaged resources will be removed and the .NET object will be ready for deleting.

Best Practice Sample

VB .NET

Dim nodeCollection As NETRONIC.XGantt.VcNodeCollection = VcGantt1.NodeCollection

Dim count As Integer = nodeCollection.Count

TryCast(nodeCollection, IDisposable).Dispose()

 

C#

VcNodeCollection nodeCollection = vcGantt1.NodeCollection;

int count = nodeCollection.Count;

(nodeCollection as IDisposable).Dispose()

 

Finally: Our Tip

See here a vivid example of how to release unmanaged resources by the Using statement that at the end internally calls Dispose().

Example with NodeCollection and GroupCollection:

        Using nodeCollection As NETRONIC.XGantt.VcNodeCollection = VcGantt1.NodeCollection, groupCollection As NETRONIC.XGantt.VcGroupCollection = VcGantt1.GroupCollection

            Dim id As String

            For Each node As NETRONIC.XGantt.VcNode In nodeCollection

                id = node.ID

            Next

            Dim count As Integer = nodeCollection.Count

            Dim groupCount As Integer = groupCollection.Count

  

       End Using

 

Now the two collections are released for the GC.

 

You do not use Version 5 of VARCHART XGantt, yet?  Download it now!

Want to learn more best practice tips & tricks for our Gantt control VARCHART XGantt? Download our free ebook.

 

Ebook- 11 Best Practice Tips for Advanced Gantt Visualization

 

Topics: Windows Forms Gantt Control, Gantt Chart Controls