Leadtools Namespace > RasterImage Class : Disposed Event |
public event EventHandler<EventArgs> Disposed
'Declaration Public Event Disposed As EventHandler(Of EventArgs)
'Usage Dim instance As RasterImage Dim handler As EventHandler(Of EventArgs) AddHandler instance.Disposed, handler
public event EventHandler<~Remove~> Disposed
add_Disposed(function(sender, e)) remove_Disposed(function(sender, e))
public: event EventHandler<EventArgs^>^ Disposed
When you create a Disposed delegate, you identify the method that handles the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate.
The Disposed event will fire whenever the Dispose method of this object is called. After this event occurs, the RasterImage object is disposed and should not be used anymore. It is recommended that you remove the handler to the Dispose event so the .NET Garbage Collector runtime can remove the object from its internal use and mark it as "unused".
If the RasterImage is being freed as a result of the .NET Garbage Collector, in other worlds, the object finalizer is being called, then this event will not occur.
Private Sub DisposedEventExample() ' Startup Dim codecs As New RasterCodecs() ' Load an image Dim image1 As RasterImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp")) ' Add a handler to the Disposed event Console.WriteLine("Adding handler to Disposed for image with format " + image1.OriginalFormat.ToString()) AddHandler image1.Disposed, AddressOf rasterImage_Disposed ' Now dispose the image Console.WriteLine("Calling .Dispose to image with format " + image1.OriginalFormat.ToString()) image1.Dispose() ' Now use the "using" syntax Using image2 As RasterImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "cannon.jpg")) ' Add a handler to the Disposed event Console.WriteLine("Adding handler to Disposed for image with format " + image2.OriginalFormat.ToString()) AddHandler image2.Disposed, AddressOf rasterImage_Disposed ' The event should occur at the next line Console.WriteLine("Will be disposed after this, format " + image2.OriginalFormat.ToString()) End Using ' Load another image Dim image3 As RasterImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif")) ' Add a handler to the Disposed event Console.WriteLine("Adding handler to Disposed for image with format " + image3.OriginalFormat.ToString()) AddHandler image3.Disposed, AddressOf rasterImage_Disposed ' Do not dispose the image Console.WriteLine("Never disposing image with format " + image3.OriginalFormat.ToString()) ' Notice that the event will never occur for image3. The image is not going to be disposed ' only its finalizer called. The garbage collector cannot guarantee that our event handler ' is still a valid object. This scenario creates resource leaks and it is recommended that ' you always dispose and object that implements the IDisposable interface, like we did ' with image1 and image2 codecs.Dispose() End Sub Private Sub rasterImage_Disposed(ByVal sender As Object, ByVal e As EventArgs) ' Get the RasterImage object being disposed Dim image As RasterImage = DirectCast(sender, RasterImage) Console.WriteLine("Being disposed, image with format " + image.OriginalFormat.ToString()) ' Remove the handler to decouple the object from our application ' and let the garbage collector take over RemoveHandler image.Disposed, AddressOf rasterImage_Disposed End Sub Public NotInheritable Class LEAD_VARS Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" End Class
private void DisposedEventExample() { // Startup RasterCodecs codecs = new RasterCodecs(); // Load an image RasterImage image1 = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir,"Image1.cmp")); // Add a handler to the Disposed event Console.WriteLine("Adding handler to Disposed for image with format " + image1.OriginalFormat); image1.Disposed += new EventHandler<EventArgs>(rasterImage_Disposed); // Now dispose the image Console.WriteLine("Calling .Dispose to image with format " + image1.OriginalFormat); image1.Dispose(); // Now use the "using" syntax using(RasterImage image2 = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir,"Master.jpg"))) { // Add a handler to the Disposed event Console.WriteLine("Adding handler to Disposed for image with format " + image2.OriginalFormat); image2.Disposed += new EventHandler<EventArgs>(rasterImage_Disposed); // The event should occur at the next line Console.WriteLine("Will be disposed after this, format " + image2.OriginalFormat); } // Load another image RasterImage image3 = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir,"Ocr1.tif")); // Add a handler to the Disposed event Console.WriteLine("Adding handler to Disposed for image with format " + image3.OriginalFormat); image3.Disposed += new EventHandler<EventArgs>(rasterImage_Disposed); // Do not dispose the image Console.WriteLine("Never disposing image with format " + image3.OriginalFormat); // Notice that the event will never occur for image3. The image is not going to be disposed // only its finalizer called. The garbage collector cannot guarantee that our event handler // is still a valid object. This scenario creates resource leaks and it is recommended that // you always dispose and object that implements the IDisposable interface, like we did // with image1 and image2 codecs.Dispose(); } private void rasterImage_Disposed(object sender, EventArgs e) { // Get the RasterImage object being disposed RasterImage image = sender as RasterImage; Console.WriteLine("Being disposed, image with format " + image.OriginalFormat); // Remove the handler to decouple the object from our application // and let the garbage collector take over image.Disposed -= new EventHandler<EventArgs>(rasterImage_Disposed); } static class LEAD_VARS { public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; }
public void DisposedEventExample() { // Create an image RasterImage image1 = new RasterImage(RasterMemoryFlags.Conventional, 100, 100, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, null, 0); // Add a handler to the Disposed event Debug.WriteLine("Adding handler to Disposed for image with format " + image1.OriginalFormat); image1.Disposed += new EventHandler<EventArgs>(rasterImage_Disposed); // Now dispose the image Debug.WriteLine("Calling .Dispose to image with format " + image1.OriginalFormat); image1.Dispose(); // Now use the "using" syntax using (RasterImage image2 = new RasterImage(RasterMemoryFlags.Conventional, 100, 100, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, null, 0)) { // Add a handler to the Disposed event Debug.WriteLine("Adding handler to Disposed for image with format " + image2.OriginalFormat); image2.Disposed += new EventHandler<EventArgs>(rasterImage_Disposed); // The event should occur at the next line Debug.WriteLine("Will be disposed after this, format " + image2.OriginalFormat); } // Load another image RasterImage image3 = new RasterImage(RasterMemoryFlags.Conventional, 100, 100, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, null, 0); // Add a handler to the Disposed event Debug.WriteLine("Adding handler to Disposed for image with format " + image3.OriginalFormat); image3.Disposed += new EventHandler<EventArgs>(rasterImage_Disposed); // Do not dispose the image Debug.WriteLine("Never disposing image with format " + image3.OriginalFormat); // Notice that the event will never occur for image3. The image is not going to be disposed // only its finalizer called. The garbage collector cannot guarantee that our event handler // is still a valid object. This scenario creates resource leaks and it is recommended that // you always dispose and object that implements the IDisposable interface, like we did // with image1 and image2 } private void rasterImage_Disposed(object sender, EventArgs e) { // Get the RasterImage object being disposed RasterImage image = sender as RasterImage; Debug.WriteLine("Being disposed, image with format " + image.OriginalFormat); // Remove the handler to decouple the object from our application // and let the garbage collector take over image.Disposed -= new EventHandler<EventArgs>(rasterImage_Disposed); }
Public Sub DisposedEventExample() ' Create an image Dim image1 As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, 100, 100, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, Nothing, Nothing, 0) ' Add a handler to the Disposed event Debug.WriteLine("Adding handler to Disposed for image with format " & image1.OriginalFormat) AddHandler image1.Disposed, AddressOf rasterImage_Disposed ' Now dispose the image Debug.WriteLine("Calling .Dispose to image with format " & image1.OriginalFormat) image1.Dispose() ' Now use the "using" syntax Using image2 As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, 100, 100, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, Nothing, Nothing, 0) ' Add a handler to the Disposed event Debug.WriteLine("Adding handler to Disposed for image with format " & image2.OriginalFormat) AddHandler image2.Disposed, AddressOf rasterImage_Disposed ' The event should occur at the next line Debug.WriteLine("Will be disposed after this, format " & image2.OriginalFormat) End Using ' Load another image Dim image3 As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, 100, 100, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, Nothing, Nothing, 0) ' Add a handler to the Disposed event Debug.WriteLine("Adding handler to Disposed for image with format " & image3.OriginalFormat) AddHandler image3.Disposed, AddressOf rasterImage_Disposed ' Do not dispose the image Debug.WriteLine("Never disposing image with format " & image3.OriginalFormat) ' Notice that the event will never occur for image3. The image is not going to be disposed ' only its finalizer called. The garbage collector cannot guarantee that our event handler ' is still a valid object. This scenario creates resource leaks and it is recommended that ' you always dispose and object that implements the IDisposable interface, like we did ' with image1 and image2 End Sub Private Sub rasterImage_Disposed(ByVal sender As Object, ByVal e As EventArgs) ' Get the RasterImage object being disposed Dim image As RasterImage = TryCast(sender, RasterImage) Debug.WriteLine("Being disposed, image with format " & image.OriginalFormat) ' Remove the handler to decouple the object from our application ' and let the garbage collector take over RemoveHandler image.Disposed, AddressOf rasterImage_Disposed End Sub
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2