简体   繁体   中英

c# program crashes/closes on minimize to system tray

I write on a little tool that should watch my server an write me mails when something went wrong like too high ram usage etc...

now to my problem, I want to minimize my Program to system tray and it works fine :) I see for some seconds the icon in the tray. after that my program is gone...closed...dont know process is away :D.

Here my code to minimize to tray:

InitializeComponent();
var icon = new NotifyIcon();
icon.Icon = new Icon("watchdog.ico");
icon.Visible = true;
icon.DoubleClick +=
       delegate(object sender, EventArgs args)
       {
           this.Show();
           this.WindowState = WindowState.Normal;
       };

protected override void OnStateChanged(EventArgs e)
       {
           if (WindowState == WindowState.Minimized)
                this.Hide();

           base.OnStateChanged(e);
       }

I hope u can help me.

On Server 2012 I was also getting an exception when minimizing to tray, but it would work fine on Windows 7 Pro computers. After I was able to remotely debug the problem was quite clear:

Exception thrown: 'System.ArgumentException' in System.Windows.Forms.dll

Additional information: Balloon tip text must have a non-empty value.

The fix is to add balloon tip text to the icon:

var icon = new NotifyIcon();
icon.BalloonTipText = "Program is minimized. Click the tray icon to restore it.";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM