.NET interprocess "events"

I have multiple instances of the same application. The user can click Finish on each instance to close it.

I would like to add a selection to โ€œQuit All Instancesโ€ that raises a bit of โ€œeventโ€ that notifies all instances of the application that they should close. I do not need to transmit data with this event.

Which is the best (and possibly the easiest) way to do this on Windows using C # /. NET?

+4
source share
2 answers

Send good'ol WM_CLOSE to all instances ...

Process[] processes = Process.GetProcesses(); string thisProcess = Process.GetCurrentProcess().MainModule.FileName; string thisProcessName = Process.GetCurrentProcess().ProcessName; foreach (Process process in processes) { // Compare process name, this will weed out most processes if (thisProcessName.CompareTo(process.ProcessName) != 0) continue; // Check the file name of the processes main module if (thisProcess.CompareTo(process.MainModule.FileName) != 0) continue; if (Process.GetCurrentProcess().Id == process.Id) { // We don't want to commit suicide continue; } // Tell the other instance to die Win32.SendMessage(process.MainWindowHandle, Win32.WM_CLOSE, 0, 0); } 
+6
source

There are several methods in the System.Diagnostics.Process class that you can use.

Process.GetProcessesByName will allow you to list all instances:

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getprocessesbyname.aspx

then Process.CloseMainWindow or Process.Close will disable them:

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.closemainwindow.aspx http://msdn.microsoft.com/en-us/library/system.diagnostics.process.close.aspx

+2
source

Source: https://habr.com/ru/post/1286290/


All Articles