Tizz
Tizz

Reputation: 840

How to have 1 notifyIcon over multiple instances of an application?

I have created a notifyIcon in my C# application and I am happy with it. Basically, my application is a wrapper for other applications (notepad, firefox, etc). So right now, when I run my app ("MyApp firefox.exe") it loads firefox and when I mouse over the icon in the task bar it says "firefox.exe" ... pretty simple.

If I run 2 instances of my application (with different parameters) I get 2 icons, one mouse over is 'firefox.exe', the other 'notepad.exe'.

What I would really like is there to be 1 icon, where I can create a ContextMenuStrip that adds all the application names as MenuItems, so when I right click, there are 2 selectable items.

Is there any way to do this without wrapping my application in yet another application wrapper? Any ideas?

Upvotes: 0

Views: 866

Answers (1)

0909EM
0909EM

Reputation: 5027

Sorry if this is a horrible answer...

Create a mutex in the same way that you to create only a single instance of an application when the first app starts...

Instead of preventing your application starting set a flag as follows

Mutex firstAppToStart = new System.Threading.Mutex(false, "Mutex...");
if (firstAppToStart.WaitOne(0, false)) {
    // set flag to allow notifyicon to be loaded
    CreateNotificationIcon();
} else {
    // The mutex is not ours, therefore we do not create a notify icon
}
// Subsequent messages will be posted to the first application to use 
// the notify icon on the first application

Now use windows messages to post between applications so now you have a single notifyicon - the other apps use windows messages to post messages to it

This does mean when the first app goes the notifyicon will also go, but you can work around this, by preventing the first app closing if it is the first app, holding a list of apps, and then posting a windows message when each closes, when they've all gone the first app can close itself...

Upvotes: 1

Related Questions