这是来自新制作的Windows Forms项目的一些代码,没有其他任何改变:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Bitmap blah = new Bitmap(16, 16);
using (Graphics blah2 = Graphics.FromImage(blah))
{
blah2.FillRectangle(new SolidBrush(Color.Black), new Rectangle(0, 0, 16, 16));
}
NotifyIcon2 n = new NotifyIcon2();
n.NotifyIcon = new NotifyIcon();
n.NotifyIcon.Icon = Icon.FromHandle(blah.GetHicon());
n.NotifyIcon.Visible = true;
}
class NotifyIcon2 : IDisposable
{
public NotifyIcon NotifyIcon { get; set; }
private bool disposed;
~NotifyIcon2()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this); // The finalise process no longer needs to be run for this
}
protected virtual void Dispose(bool disposeManagedResources)
{
if (!disposed)
{
try
{
NotifyIcon.Dispose();
}
catch { }
disposed = true;
}
}
}
}
从我所看到的,在protected virtual void Dispose
中,NotifyIcon
在执行时已被处理掉(解释为什么我在那里放置了try / catch块),所以我无能为力关于它的图标。
那我怎么让它消失呢?
答案 0 :(得分:1)
原来处理父类会将孩子带走。
FormClosing += (sender, e) => n.Dispose();
(参见BoltClock对该问题的评论中提供的链接)