Hello to everyone! I am moving from AutoIt to C#, Im doing some little projects like file renaming, moving, automatizing some actions and stuff.
I did a Form for music management, it turned out pretty good but there is a small issue, I need to kill the process “wmplayer” and re-open it later under some conditions, so with some research I found that the most common way is using this code


foreach (var process in Process.GetProcessesByName("wmplayer"))
{
process.Kill();
}
The problem is my Windows Media Player has some plugins, that when closed in this way (kinda like CTRL-ALT-DEL) they give out error like the WMP crashed or something, and I need to re-enable all of them manually.
So since the action of closing the process is literally called “.Kill”, is there any “softer” way to close it, like something equivalent than pressing the X on the WMP window, instead of brutally killing it?
Thanks, any help is appreciated : )
Greetings to everyone
Process.CloseMainWindow() sends WM_CLOSE message to the main window which gracefully closes the program.
Problem is its not guaranteed to close the application, for example some applications show a prompt before exiting etc.
So after calling it better to check if process is closed after sometime and Kill if not.
This is working very good, I just tested it! At least with WMP, it doesn’t show any prompt before exiting (unless I modify the music while playing or other stuff, thats obvious).
Thanks a lot! 🙂
You can Post WM_CLOSE message to Main window of target process.
Is there any “softer” way to close it, like something equivalent than pressing the X on the WMP window, instead of brutally killing it?
I think you should be able to use [Process.MainWindowHandle] (https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.mainwindowhandle) and send it a WM_CLOSE message. (By P/Invoke on SendMessage or PostMessage from Win32 if there’s no .NET wrapper for this.)
Try Process.Euthanize()
Process.SendToHeaven()
There really should be a Process.HeyBuddyShutItDown()
Process.Kevorkian();


C# devs
null reference exceptions

source