NotifyIcon remains in Tray even after application closing but disappears on Mouse Hover

后端 未结 14 1205
清歌不尽
清歌不尽 2020-12-03 00:08

There are many questions on SO asking same doubt. Solution for this is to set

notifyIcon.icon = null and calling Dispose for it in FormClo

相关标签:
14条回答
  • 2020-12-03 00:47

    Try Application.DoEvents(); after setting notifyIcon.Icon to null and disposing:

    notifyIcon.Icon = null;
    notifyIcon.Dispose();
    Application.DoEvents();
    

    And consider Environment.Exit(0); instead of Process.GetCurrentProcess().Kill().

    0 讨论(0)
  • 2020-12-03 00:53

    The only way that works to me was:

    1. On design screen changing notifyicon1 property visible=false

    2. Insert the code below on main form "activated" event:

    NotifyIcon1.Visible = True
    
    1. Insert the code below on main form "closing" event:
    NotifyIcon1.Visible = false
    NotifyIcon1.Icon.Dispose()
    NotifyIcon1.Dispose()
    
    0 讨论(0)
  • 2020-12-03 00:54

    I had the exact same problem as you.

    The proper way are send WM_CLOSE message to a process.
    I use the c# code I found in this article.
    http://social.msdn.microsoft.com/Forums/vstudio/en-US/82992842-80eb-43c8-a9e6-0a6a1d19b00f/terminating-a-process-in-a-friendly-way

    0 讨论(0)
  • 2020-12-03 00:54

    edit codes of ...Designer.cs as below coding.

            protected override void Dispose(bool disposing)
               {
               if (disposing )
                   {
                   this.notifyicon.Dispose();
                   }
               base.Dispose(disposing);
               }
    
    0 讨论(0)
  • 2020-12-03 00:56

    Components just must be disposed in the right order like this :

    NotifyIcon.Icon.Dispose();
    
    NotifyIcon.Dispose();
    

    Add this to the MainWindow closing event.

    Hope this will help.

    0 讨论(0)
  • 2020-12-03 00:58

    I don't think WPF has it's own NotifyIcon, does it? If you're using the 3rd party Harcodet.Wpf.TaskbarNotification, then try this:

    In order to prevent my app from closing when the window is closed (run in background), I separated the logic for closing the window (hitting the x button in the upper right) and actually shutting it down (through the context menu). To make this work, make your context menu set _isExplicitClose to true. Otherwise, it'll just hide the window and continue to run.

    What this does is, on explicit close, hide tray icon and the form before closing. This way the icon isn't hanging around after the application is shutdown.

    private bool _isExplicitClose;
    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    {
        base.OnClosing(e);
    
        if (!_isExplicitClose)
        {
            e.Cancel = true;
            Hide();
        }
    }
    
    protected void QuitService(object sender, RoutedEventArgs e)
    {
       _isExplicitClose = true;
       TaskbarIcon.Visibility = Visibility.Hidden;
       Close();
    }
    
    0 讨论(0)
提交回复
热议问题