Exit all instances of my app

c#, winforms

Solution

You can terminate all processes with the same name:

var current = Process.GetCurrentProcess();
Process.GetProcessesByName(current.ProcessName)
    .Where(t => t.Id != current.Id)
    .ToList()
    .ForEach(t => t.Kill());

current.Kill();

Problem

I have the following function: ``` private void TakeOverAllScreens() { int i = 0; foreach (Screen s in Screen.AllScreens) { if (s != Screen.PrimaryScreen) { i++; Process.Start(Application.ExecutablePath, "Screen|" + s.Bounds.X + "|" + s.Bounds.Y + "|" + i); } } } ``` As you can see it creates a separate instance of my application for each screen on the pc. How can I create an exit function that closes all of them? The function needs to work on any instance of the app, not just my "Main" instance.

Original source

Related problems