How can I make the icon associated with my program appear in the system tray only when it is running?

Thanks.