title: Changing System Tray Icon Image
body: <p>I have built a Tray Application in .Net which works fine. However, users want to change Tray Icon image at runtime on certain conditions.  To make it simple, let us say, something is not working - Tray Icon should show Red image; if everything is fine, it should show green.  I'm not sure how to achieve this in .Net.  </p>
  
  <p>Please provide some inputs on this.  Thanks</p>
  
  <p>I built CustomApplicationContent for Tray.  Some snippets below:</p>
  
  <p>Program.cs</p>
  
  <pre><code>[STAThread]
      static void Main()
      {
          if (!SingleInstance.Start()) { return; }
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          try
          {
              var applicationContext = new CustomApplicationContext();
              Application.Run(applicationContext);
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message, "Program Terminated Unexpectedly",
                  MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
          SingleInstance.Stop();
      }
  </code></pre>
  
  <p>CustomApplicationContext.cs</p>
  
  <pre><code>public class CustomApplicationContext : ApplicationContext
  {
      private System.ComponentModel.IContainer components;    // a list of components to dispose when the context is disposed
      private NotifyIcon notifyIcon;
      private static readonly string IconFileName = "green.ico";
      private static readonly string DefaultTooltip = "Employee Management System";
      private readonly TrayManager trayManager;
  
      public CustomApplicationContext()
      {
          InitializeContext();
          trayManager = new TrayManager(notifyIcon);
      }
  
      protected override void Dispose(bool disposing)
      {
          if (disposing &amp;&amp; components != null) { components.Dispose(); }
      }
  
  
      private void InitializeContext()
      {
          components = new System.ComponentModel.Container();
          notifyIcon = new NotifyIcon(components)
          {
              ContextMenuStrip = new ContextMenuStrip(),
              Icon = new Icon(IconFileName),
              Text = DefaultTooltip,
              Visible = true
          };
          notifyIcon.ContextMenuStrip.Opening += ContextMenuStrip_Opening;
          notifyIcon.DoubleClick += notifyIcon_DoubleClick;
          //notifyIcon.MouseUp += notifyIcon_MouseUp;
      }
  private void notifyIcon_DoubleClick(object sender, EventArgs e)
      {
          ShowAboutForm();
      }
  private TestForm testForm;
  
      private void ShowAboutForm()
      {
          if (testForm == null)
          {
              testForm = new TestForm { trayManager = trayManager };
              testForm.Closed += testForm_Closed; ; // avoid reshowing a disposed form
              testForm.Show();
          }
          else { testForm.Activate(); }
      }
  
  
      void testForm_Closed(object sender, EventArgs e)
      {
          testForm = null;
      }
  </code></pre>
  
  <p>Where do I add timer - in Context?  Users may not open a form so adding timer on Form may not work all the time.  How do I change icon?</p>