为什么我不能让在另一个类中实现的 NotifyIcon 在退出时消失?

发布于 2024-10-06 05:35:26 字数 1417 浏览 3 评论 0原文

以下是来自新制作的 Windows 窗体项目的一些代码,没有任何其他更改:

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 块),所以我无法对其图标执行任何操作。

那么我该如何让它消失呢?

Here's some code from a freshly made Windows Forms project, with nothing else changed:

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;
            }
        }
    }
}

From what I can see, in protected virtual void Dispose, the NotifyIcon has already been disposed when it gets executed (explaining why I put a try/catch block there), so I can't do anything about its icon.

So how do I make it disappear?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2024-10-13 05:35:26

事实证明,处理父类会同时删除子类。

FormClosing += (sender, e) => n.Dispose();

(请参阅 BoltClock 对问题的评论中提供的链接)

Turns out disposing of the parent class will take out the child along with it.

FormClosing += (sender, e) => n.Dispose();

(see the link provided in BoltClock's comment on the question)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文