I have an application that runs 8-10 times at the same time. When this application runned for the first time, I created a notifyIcon1
in the system tray, and not in the next runs. I also added a menu to this notifyIcon1
. However, the menu is active only the first runned application. What should I do to control all opened applications from this menu?
The application name is WindowsFormsApp1.exe
and the codes are as follows:
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TrayMenuContext();
}
private void Form1_Load(object sender, EventArgs e)
{
var count = Process.GetProcessesByName("WindowsFormsApp1").Length;
notifyIcon1.Visible = count == 1;
}
private void TrayMenuContext()
{
notifyIcon1.ContextMenuStrip = new ContextMenuStrip();
notifyIcon1.ContextMenuStrip.Items.Add("Close all apps...", null, notifyIcon1_Close_Click);
notifyIcon1.ContextMenuStrip.Items.Add("Minimize all apps...", null, notifyIcon1_Minimize_Click);
notifyIcon1.ContextMenuStrip.Items.Add("Show all apps...", null, notifyIcon1_Show_Click);
}
void notifyIcon1_Close_Click(object sender, EventArgs e)
{
Close();
}
void notifyIcon1_Minimize_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}
void notifyIcon1_Show_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Normal;
}
}
}