1

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.

user2244925
  • 2,314
  • 3
  • 14
  • 11
  • 2
    Without an actual exception, it's hard to tell what's going on. You should attach a debugger and break on exceptions. – Daniel Hilgarth Apr 04 '13 at 12:57
  • 2
    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 Apr 04 '13 at 12:57
  • 1
    You should get some hint on what happened in Event Viewer. – aybe Apr 04 '13 at 12:59
  • 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 Apr 04 '13 at 13:07
  • 1
    Are you minimizing to the tray when you press X? If so, you also need to trap the Form_Closing event: http://stackoverflow.com/questions/2865279/stop-the-form-from-closing-on-close-button-click – Surfbutler Apr 04 '13 at 13:28
  • Possible duplicate of http://stackoverflow.com/questions/13625069/c-sharp-minimize-to-system-tray-on-close?rq=1 – Surfbutler Apr 04 '13 at 13:37

1 Answers1

0

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.";
Quantic
  • 1,779
  • 19
  • 30