Is the icon still available after calling Dispose ()?

I am trying to deal with some of our code using Dispose correctly in a number of places where everything remained hanging. Once such an instance was a badge, and something that I noticed, I thought it was strange, if I call Icon.Dispose(), I could still use the badge.

So, I extracted it from a small console application, which I was fully expecting to crash (throwing an ObjectDisposedException), but this is not ... I misunderstand what to do here?

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;

namespace DisposeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Icon icon = new Icon(@"C:\temp\test.ico");
            icon.ToBitmap().Save(@"C:\temp\1.bmp");
            icon.Save(new FileStream(@"C:\temp\1.ico", FileMode.OpenOrCreate, FileAccess.ReadWrite));
            icon.Dispose();
            GC.Collect(); // Probably not needed, but just checking.
            icon.Save(new FileStream(@"C:\temp\2.ico", FileMode.OpenOrCreate, FileAccess.ReadWrite));
            icon.ToBitmap().Save(@"C:\temp\2.bmp");
        }
    }
}
+5
source share
4 answers

, Dispose() . Icon . []. , , .

- Windows. , Icon.FromHandle() System.Drawing.SystemIcons. , Dispose() .

- . , Dispose() , . MemoryStream BackgroundWorker. , .

+6

, , . , ?

, , . , , , . , , " , ", , .

( , - , Connect, .)

, , . , C:

?

, , , , , . , "" - . - . ( , , ​​ ), - .

+8

, , . Dispose() ; " " . , , HICON Dispose(). () , Icon ( byte[]), .

You try to do this by calling GC.Collect(), but do nothing, because you Iconare still alive: it is referenced by a local variable with the name Iconand currently does not have the right to collect it.

+4
source

Take a look at the using statement. It should be used every time an object is created that implements IDisposable. It ensures that the object is correctly deleted even if an exception is thrown.

http://msdn.microsoft.com/en-us/library/yh598w02.aspx

0
source

All Articles