Hi! Here's my issue: I need to close a process, already running, from a C# program. The problem is that the process now runs as an icon (minimized to taskbar), and unless the user opens it at least once (which will never happen on unattended machines), it'll never have a main window.
The other requirement that I have is that the application be closed not killed. I need it to write it's memory buffers to disk - and killing it causes data loss.
Here's what I tried so far:
foreach (Process proc in Process.GetProcesses())
{
if (proc.ProcessName.ToLower().StartsWith("myapp"))
{
if (proc.MainWindowHandle.ToInt32() != 0)
{
proc.CloseMainWindow();
proc.Close();
//proc.Kill(); <--- not good!
}
}
}
I've added the if clause, after discovering that MainWindowHandle == 0 when the window was minimized. Removig the if doesn't help. Neither the CloseMainWindow() nor the Close() work. The Kill() does, but as mentioned above - it's not what I need.
Any idea would be accepted, including the use of arcane Win32 API functions :)