S h o r t S t o r i e s

// Tales from software development

Creating a tray icon application using NotifyIcon

leave a comment »

There are numerous code examples of how to create a tray icon application using the .NET NotifyIcon class. Many claim to show the ‘proper’ way to do this by using a hidden Windows Form as the main component of the application.

It’s easy to see why some might believe this to be true as the Form class implements IDispose methods and an IContainer for controls such as the System.Windows.Forms Timer control (as opposed to other Timer controls such as System.Timers namespace) that might be useful in a tray application.

However, there really isn’t any need to use a Form and, arguably, it adds problems of its own.

All that’s required is that you implement IDisposable and, if you want, an IContainer:

public class MonitorTrayApp : IDisposable
{
  /// <summary>
  /// Container for the form's components.
  /// </summary>
  private IContainer components = null;

  /// <summary>
  /// The NotifyIcon instance.
  /// </summary>
  private NotifyIcon notifyIcon = null;

  /// <summary>
  /// A timer control to drive the polling of the Vitalpulse Monitor service.
  /// </summary>
  private Timer intervalTimer = null;
 
  /// <summary>
  /// Initializes a new instance of the MonitorTrayApp class.
  /// </summary>
  public MonitorTrayApp()
  {
    this.InitializeComponent();
  }
 
  /// <summary>
  /// Initialises the system tray icon.
  /// </summary>
  private void InitializeComponent()
  {
    this.components = new Container();

    this.notifyIcon = new NotifyIcon(this.components);

    this.intervalTimer = new Timer(this.components);
    this.intervalTimer.Tick += new EventHandler(this.IntervalTimerTick);
    this.intervalTimer.Interval = Settings.Default.PollInterval * 1000;
    this.intervalTimer.Enabled = true;

    this.notifyIcon.Icon = Resources.AppIcon;
    this.notifyIcon.Text = Application.ProductName;
    this.notifyIcon.Visible = true;
  }
 
  /// <summary>
  /// Tick event handler for the interval timer control.
  /// </summary>
  /// <param name="sender">The object raising the event.</param>
  /// <param name="e">The event args.</param>
  private void IntervalTimerTick(object sender, EventArgs e)
  {
    // Do some processing here...
  } 
 
  /// <summary>
  /// Performs application IDisposable processing.
  /// </summary>
  public void Dispose()
  {
    this.Dispose(true);
  }

  /// <summary>
  /// Performs application IDisposable processing.
  /// </summary>
  /// <param name="disposing"></param>
  protected virtual void Dispose(bool disposing)
  {
    if (disposing)
    {
      if (this.components != null)
      {
        this.components.Dispose();
      }

      this.notifyIcon.Dispose();
    }
  }
}

The application’s entry point is a Main() method:

public static class Program
{
  [STAThread]
  public static void Main()
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    using (MonitorTrayApp notifierTrayIcon = new MonitorTrayApp())
    {
      Application.Run();
    }
  }
}

Written by Sea Monkey

January 18, 2017 at 8:00 pm

Posted in Development

Tagged with ,

Leave a comment