From 76796ca52a69cfce922be253d6e9207ccbfd09a7 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Tue, 4 Sep 2018 17:01:08 +0300 Subject: Started working on Test Studio... Machine Studio v3.4.42 --- .../ExtensionMethods/ProcessExtensions.cs | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Software/Visual_Studio/Tango.Core/ExtensionMethods/ProcessExtensions.cs (limited to 'Software/Visual_Studio/Tango.Core/ExtensionMethods') diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ProcessExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/ProcessExtensions.cs new file mode 100644 index 000000000..43bf0f5a3 --- /dev/null +++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/ProcessExtensions.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +public static class ProcessExtensions +{ + private static string FindIndexedProcessName(int pid) + { + var processName = Process.GetProcessById(pid).ProcessName; + var processesByName = Process.GetProcessesByName(processName); + string processIndexdName = null; + + for (var index = 0; index < processesByName.Length; index++) + { + processIndexdName = index == 0 ? processName : processName + "#" + index; + var processId = new PerformanceCounter("Process", "ID Process", processIndexdName); + if ((int)processId.NextValue() == pid) + { + return processIndexdName; + } + } + + return processIndexdName; + } + + private static Process FindPidFromIndexedProcessName(string indexedProcessName) + { + var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName); + return Process.GetProcessById((int)parentId.NextValue()); + } + + /// + /// Gets the executing process. + /// + /// The process. + /// + public static Process Parent(this Process process) + { + return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id)); + } +} -- cgit v1.3.1