Close all instances of my application.

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 to create an exit function that closes all of them? The function should work on any instance of the application, and not just on my "main" instance.

+8
c # winforms
source share
6 answers

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(); 
+7
source share

You just need to track open processes:

 List<Process> opened = new List<Process>(); private void TakeOverAllScreens() { int i = 0; foreach (Screen s in Screen.AllScreens) { if (s != Screen.PrimaryScreen) { i++; opened.Add(Process.Start(Application.ExecutablePath, "Screen|" + s.Bounds.X + "|" + s.Bounds.Y + "|" + i)); } } } 

Then:

 private void terminateAll() { foreach (var p in opened) p.Kill(); } 
+3
source share

How to close the application?

There are three general options:

  • Kill him right.
  • Assuming that it has a main window and that it behaves traditionally, send a WM_CLOSE message to that window using P / Invoking SendMessage
  • Assuming he wants to collaborate, use your own communication channel to tell him to complete himself

How to close the application?

Killing a process is completely difficult and should never be undertaken unless you know that this process will not do anything sensitive when it stops. This is usually used as a last resort.

To close the main window, you first need to find out what exactly this HWND window is; You can do this by going through the list of top-level windows or by asking the process associated with it to somehow inform you about it. It also suggests that another process will decide to cease to exist when you ask to close its main window (in fact, it may decide to do something, including completely ignoring the message). This approach is a reasonable first attempt, and it usually works fine with processes that are not under your control - although you need to find the window yourself.

Using a custom communication channel allows you to fully control what is happening and how it can be implemented in many ways, but this is an approach that involves recording most of the code and requires that you have access to the source for both applications.

Conclusion

It depends on . If you are 1000% sure that killing the process will not harm anything, it will be a quick and dirty decision; otherwise, you need to use one of two β€œpolite” approaches.

+3
source share

Put the whole open process on the list so you can kill the process one by one. Check out the code below:

 private void TakeOverAllScreens() { int i = 0; List<Process> allProcesses = new List<Process>(); foreach (Screen s in Screen.AllScreens) { if (s != Screen.PrimaryScreen) { i++; allProcesses.Add(Process.Start(Application.ExecutablePath, "Screen|" + s.Bounds.X + "|" + s.Bounds.Y + "|" + i)); } } foreach (Process proc in allProcesses) { proc.Kill(); } } 
+1
source share

Try this feature:

 void ExitAll(string processName) { foreach(Process p in Process.GetProcesses()) { if (p.ProcessName.ToLower().Equals(processName.ToLower())) p.Kill(); } } 
+1
source share

I always execute the command

 tskill notepad // to kill all instances of notepad 

You can do this inside the application, for example

 Process.Start("cmd /k tskill " + Process.GetCurrentProcess().ProcessName) 
+1
source share

All Articles