1
votes

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.

1
Without an actual exception, it's hard to tell what's going on. You should attach a debugger and break on exceptions. - Daniel Hilgarth
OK, have you tried debugging and getting some kind of exception? If you can't do that, implement some kind of logging tool. Having a stack trace and an error message will help a lot in figuring this out. - tnw
You should get some hint on what happened in Event Viewer. - aybe
There is no exception mhhh I got an idea. It is the normal "X" Button on the upper right side. Is it possible that the program dont know that it should not end? And where can I change it. - user2244925
Are you minimizing to the tray when you press X? If so, you also need to trap the Form_Closing event: stackoverflow.com/questions/2865279/… - Surfbutler

1 Answers

0
votes

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.";