diff --git a/src/Core/WinSWCore/Util/ProcessHelper.cs b/src/Core/WinSWCore/Util/ProcessHelper.cs
index 5554a20..bfa8326 100644
--- a/src/Core/WinSWCore/Util/ProcessHelper.cs
+++ b/src/Core/WinSWCore/Util/ProcessHelper.cs
@@ -22,15 +22,23 @@ namespace winsw.Util
/// List of child process PIDs
public static List GetChildPids(int pid)
{
- var searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid);
- var childPids = new List();
- foreach (var mo in searcher.Get())
+ var childPids = new List();
+
+ try {
+ var searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid);
+ foreach (var mo in searcher.Get())
+ {
+ var childProcessId = mo["ProcessID"];
+ Logger.Info("Found child process: " + childProcessId + " Name: " + mo["Name"]);
+ childPids.Add(Convert.ToInt32(childProcessId));
+ }
+ }
+ catch (Exception ex)
{
- var childProcessId = mo["ProcessID"];
- Logger.Info("Found child process: " + childProcessId + " Name: " + mo["Name"]);
- childPids.Add(Convert.ToInt32(childProcessId));
+ Logger.Warn("Failed to locate children of the process with PID=" + pid + ". Child processes won't be terminated", ex);
}
- return childPids;
+
+ return childPids;
}
///
@@ -90,19 +98,9 @@ namespace winsw.Util
/// If enabled, the perent process will be terminated before its children on all levels
public static void StopProcessAndChildren(int pid, TimeSpan stopTimeout, bool stopParentProcessFirst)
{
- List childPids = null;
- try
- {
- childPids = GetChildPids(pid);
- }
- catch (Exception ex)
- {
- Logger.Warn("Failed to locate children of the process with PID=" + pid + ". Child processes won't be terminated", ex);
- }
-
- if (!stopParentProcessFirst && childPids != null)
- {
- foreach (var childPid in childPids)
+ if (!stopParentProcessFirst)
+ {
+ foreach (var childPid in GetChildPids(pid))
{
StopProcessAndChildren(childPid, stopTimeout, stopParentProcessFirst);
}
@@ -110,9 +108,9 @@ namespace winsw.Util
StopProcess(pid, stopTimeout);
- if (stopParentProcessFirst && childPids != null)
+ if (stopParentProcessFirst)
{
- foreach (var childPid in childPids)
+ foreach (var childPid in GetChildPids(pid))
{
StopProcessAndChildren(childPid, stopTimeout, stopParentProcessFirst);
}
diff --git a/src/Core/WinSWCore/Util/SigIntHelper.cs b/src/Core/WinSWCore/Util/SigIntHelper.cs
index 87e19f6..9fa30b0 100644
--- a/src/Core/WinSWCore/Util/SigIntHelper.cs
+++ b/src/Core/WinSWCore/Util/SigIntHelper.cs
@@ -49,6 +49,9 @@ namespace winsw.Util
process.WaitForExit((int)shutdownTimeout.TotalMilliseconds);
+ // Detach from console. Causes child console process to be automatically closed.
+ FreeConsole();
+
return process.HasExited;
}