Properly Disposing of Resources in .NET


FileUp is a COM component that provides .NET support. It is not a pure .NET assembly. As such, it contains both "managed" and "unmanaged" code. When using components of this nature on the .NET framework, you must explicitly dispose of the object in order to free up resources used by that object.

This is best done by declaring the FileUp object before a Try...Catch...Finallyblock, instantiating FileUp within the Try block and disposing of FileUp within the Finally block.


C# using SoftArtisans.Net;


...(namespace, class, and other declaration code, )....

private void ProcessUpload()
{
//--- Declare the FileUp object
FileUp fileUpload = null;

try
{
fileUpload = new FileUp(Context);

...(upload processing code)

     }
catch (Exception ex)
{

...(error handling code here)

     }
finally
{
//--- Always call Dispose in the finally block
if (fileUpload != null)
fileUpload.Dispose();
fileUpload = null;
}
}
VB.NET Imports SoftArtisans.Net

...(namespace, class, and other declaration code, )....

Private Sub ProcessUpload()
'--- Declare the FileUp object
Dim fileUpload As FileUp

Try
fileUpload = New FileUp(Context)

...(upload processing code)

     Catch ex As System.Exception

...(error handling code here)

     Finally
'--- Always call FileUp.Dispose in the finally block
If Not (fileUpload Is Nothing) Then
fileUpload.Dispose()
End If
fileUpload = Nothing
End Try
End Sub

(...)

Copyright © 2010 SoftArtisans, Inc. All rights reserved.