Click here to Skip to main content
15,123,118 members
Articles / Programming Languages / C#
Posted 6 May 2007


124 bookmarked

Creating a Tasktray Application

Rate me:
Please Sign up or sign in to vote.
4.43/5 (35 votes)
23 Sep 2007CPOL2 min read
How to create an application that consists primarily of a tasktray icon
Screenshot - TaskTrayApp.png


Quite often, people find themselves writing small applications that live mainly in the TaskTray as a TaskTray icon. Something that monitors something, updates something, little applications that run in the background but that they want to let the user know it's running.

To date, the method I've seen all around the Internet to achieve this is to create the configuration form (which in turn contains the NotifyIcon) and then hide the configuration form.

This, however, has several drawbacks, the main one being that the configuration form will flicker whenever you start the application. Getting it to start hidden can be a bit of a chore.

Fortunately, there is a better way to start an application as a NotifyIcon: using ApplicationContext.

Using the Code

Start by creating a WinForms project and editing the configuration form to suit your needs. Once you have done this, open up Program.cs. You should find that the last line looks like this:

Application.Run(new Form1());

This tells the Application to load and show Form1, and to keep the Application alive as long as Form1 is alive. But take a look at the overloads for Run. Not only does it accept Forms, but also ApplicationContexts.

Create a new class that inherits from ApplicationContext (we will refer to it as MyApplicationContext). Then replace the above line with:

Application.Run(new MyApplicationContext());

Inside the constructor for MyApplicationContex, you can insert the code to initialize the NotifyIcon. Mine looks something like this:

MenuItem configMenuItem = new MenuItem("Configuration", new EventHandler(ShowConfig));
MenuItem exitMenuItem = new MenuItem("Exit", new EventHandler(Exit));

NotifyIcon notifyIcon = new NotifyIcon();
notifyIcon.Icon = TaskTrayApplication.Properties.Resources.AppIcon;
notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] 
    { configMenuItem, exitMenuItem });
notifyIcon.Visible = true;

You can then create a ShowConfig method to display the configuration window. Mine looks something like this:

Configuration configWindow = new Configuration();
void ShowConfig(object sender, EventArgs e)
    // If we are already showing the window, merely focus it.
    if (configWindow.Visible)

And for completeness, my Exit method looks like this:

void Exit(object sender, EventArgs e)
    // We must manually tidy up and remove the icon before we exit.
    // Otherwise it will be left behind until the user mouses over.
    notifyIcon.Visible = false;

So you should now be able to create applications that start, live and die in the TaskTray without having to hack together a form to host the NotifyIcon.


  • 07/05/2007
    • Submitted article
  • 09/23/2007
    • Fixed formatting
    • Changed configWindow.Focus(); to configWindow.Activate();


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

QuestionStill works in 2020 Pin
chilipepper98721-Oct-20 9:06
Memberchilipepper98721-Oct-20 9:06 
GeneralKudos for such a simple and self-explanatory app Pin
Dan Ghiorghita20-May-20 0:32
MemberDan Ghiorghita20-May-20 0:32 
GeneralMy vote of 5 Pin
Dan Ghiorghita20-May-20 0:30
MemberDan Ghiorghita20-May-20 0:30 
Praisethanks! Pin
Member 1405819217-Nov-18 17:46
MemberMember 1405819217-Nov-18 17:46 
Praisehelped Pin
Mike Azazel23-May-16 2:14
MemberMike Azazel23-May-16 2:14 
GeneralThanks! [modified] Pin
Don Mattéo29-Sep-07 14:47
MemberDon Mattéo29-Sep-07 14:47 
QuestionLeft Click Context Menu Pin
Skylinc27-Sep-07 10:32
MemberSkylinc27-Sep-07 10:32 
AnswerRe: Left Click Context Menu Pin
Skylinc27-Sep-07 11:29
MemberSkylinc27-Sep-07 11:29 
QuestionCool, only a gripe left Pin
CarlosMPerez25-Sep-07 23:27
MemberCarlosMPerez25-Sep-07 23:27 
AnswerRe: Cool, only a gripe left Pin
[ICR]26-Sep-07 22:50
Member[ICR]26-Sep-07 22:50 
GeneralCool Pin
Andreo25-Sep-07 4:20
MemberAndreo25-Sep-07 4:20 
GeneralClean.Simple.Straightforward Pin
dedgod9-Jul-07 5:58
Memberdedgod9-Jul-07 5:58 
GeneralAwesome. Pin
Marc Leger27-Jun-07 9:43
MemberMarc Leger27-Jun-07 9:43 
QuestionVery nice, but can I start a message-loop without any forms? Pin
david zha020-May-07 20:46
Memberdavid zha020-May-07 20:46 
AnswerRe: Very nice, but can I start a message-loop without any forms? Pin
[ICR]20-May-07 23:52
Member[ICR]20-May-07 23:52 
AnswerRe: Very nice, but can I start a message-loop without any forms? Pin
david zha021-May-07 2:08
Memberdavid zha021-May-07 2:08 
GeneralVery Cool Pin
Mike DiRenzo7-May-07 10:38
MemberMike DiRenzo7-May-07 10:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.