0

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;
        }
    }
}
Quince
  • 144
  • 11
  • I would probably start by looping through all the processes looking for your running applications. – LarsTech Mar 29 '20 at 19:01
  • @LarsTech - I tried ´Process.GetProcessesByName("WindowsFormsApp1")´ but it not contain `Form` so for example `WindowState = FormWindowState.Normal;` not work. – Quince Mar 29 '20 at 19:53
  • This is not the answer to my question. Because 'WindowsFormsApp1' applications we open will have so much 'ProcessById'. And every time the application is opened, it will get a different `ProcessById. – Quince Mar 29 '20 at 20:30
  • Right. So loop through them and find the reference to the form handle. – LarsTech Mar 29 '20 at 22:07

0 Answers0