title: .NET NotifyIcon randomly disappearing
body: <p>I have a WPF application that consists of a long running main window.  In the <code>MainWindow</code> constructor I define a Notify icon:</p>
  
  <pre><code>private System.Windows.Forms.NotifyIcon notifyIcon;
  
  public MainWindow()
  {
     InitializeComponent();
  
     System.Windows.Forms.ContextMenu notifyMenu = new System.Windows.Forms.ContextMenu();
     System.Windows.Forms.MenuItem notifyMenuItem = new System.Windows.Forms.MenuItem();
     notifyMenuItem.Text = "Exit MainWindow";
     notifyMenuItem.Click += new EventHandler(notifyMenuItem_Click);
     notifyMenu.MenuItems.Add(notifyMenuItem);
     notifyIcon = new System.Windows.Forms.NotifyIcon();
     notifyIcon.BalloonTipText = "MainWindow has been minimized. Click the tray icon to show.";
     notifyIcon.BalloonTipTitle = "MainWindow";
     notifyIcon.Text = "MainWindow";
     notifyIcon.Icon = new System.Drawing.Icon("some.ico");
     notifyIcon.Visible = true;
     notifyIcon.Click += new EventHandler(notifyIcon_Click);
     notifyIcon.ContextMenu = notifyMenu;
  }
  </code></pre>
  
  <p>This is all good and well, except that sometimes the <code>NotifyIcon</code> simply disappears from the tray.  Absolutely nowhere in the code is <code>notifyIcon.Visible = false;</code> set as the <code>NotifyIcon</code> should also be visible.  My application (by requirement) does not show up in the taskbar and uses a single instance manager (if another instance starts up, the newer instance is required to abort immediately).  So this leads to a situation where the application was minimized to the tray, the NotifyIcon disappears and the user things the application is not running (it is) and they cannot start a new one (single instance mechanism).</p>
  
  <p>I've seen some SO posts on this matter but they seem to blame the NotifyIcon being instantiated somewhere other than the main windows, which does not apply to my case.  Are there other reasons for flaky NotifyIcon behavior, as well as remedies?</p>