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();
icon.Save(new FileStream(@"C:\temp\2.ico", FileMode.OpenOrCreate, FileAccess.ReadWrite));
icon.ToBitmap().Save(@"C:\temp\2.bmp");
}
}
}
source
share